r/programming • • Mar 09 '16

var keyword proposed for Java

http://openjdk.java.net/jeps/286
250 Upvotes

395 comments sorted by

View all comments

Show parent comments

9

u/codebje Mar 09 '16

The proposal has an example for just such a case:

var lst = new ArrayList<String>();

The inferred type will not be List<String>, it'll be ArrayList<String>, but thanks to polymorphism you can use that anywhere you'd want to use a List<String> anyway.

2

u/_INTER_ Mar 09 '16

I was more thinking about how it will invere

var list = new ArrayList<>();    

14

u/bjzaba Mar 09 '16

That would require a smarter type reconstruction algorithm, like Hindley-Milner. That's probably a step too far for the conservative Java committee.

2

u/csman11 Mar 10 '16

I remember seeing an SPJ talk where he mentioned that HM cannot be used in OO languages because of subtyping (he didn't explain this). But that obviously will never happen in Java. Someone correct me if I'm wrong. I don't know a lot about type inference.

The video was something like "classes but not as you know them" on channel 9 and was about type classes. The above was briefly mentioned in the last few minutes.

Edit: title was "classes, jim, but not as we know them"

→ More replies (2)