Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a programming language where the static data type is optional?

I'm thinking something where the following two are equivalent:

 int [] array = { 1,2,3,4 }
 foreach( int i in array ) {
    print i 
 }


 array = { 1,2,3,4 }
 foreach( i in array ) {
     print i 
 }
like image 403
OscarRyz Avatar asked Jul 04 '26 11:07

OscarRyz


2 Answers

Yes, languages that have type inference work this way. The code is statically typed, but the compiler can figure out that if you write { 1,2,3,4 }, then anything you assign that to, or call with that, is of type int[]. It saves a lot of (finger) typing.

like image 195
Adam Goode Avatar answered Jul 09 '26 15:07

Adam Goode


C# allows you to use the var keyword in declarations to infer the type from the right hand side of the assignment.

like image 28
Greg Hewgill Avatar answered Jul 09 '26 14:07

Greg Hewgill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!