MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/programming/comments/49prtn/var_keyword_proposed_for_java/d0ulc8x/?context=3
r/programming • u/adila01 • Mar 09 '16
395 comments sorted by
View all comments
17
How will this deal with code like:
List<String> lst = new ArrayList<>();
I always wondered why they've choosen to abbreviate the RHS instead of the LHS (in Java 7 I think). Clear me up please.
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 inverevar list = new ArrayList<>(); 2 u/Eirenarch Mar 10 '16 This won't compile. You will usevar list = new ArrayList<String>();instead. There is no benefit from the diamond syntax in the presence of var.
9
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.
List<String>
ArrayList<String>
2 u/_INTER_ Mar 09 '16 I was more thinking about how it will inverevar list = new ArrayList<>(); 2 u/Eirenarch Mar 10 '16 This won't compile. You will usevar list = new ArrayList<String>();instead. There is no benefit from the diamond syntax in the presence of var.
2
I was more thinking about how it will invere
var list = new ArrayList<>();
2 u/Eirenarch Mar 10 '16 This won't compile. You will usevar list = new ArrayList<String>();instead. There is no benefit from the diamond syntax in the presence of var.
This won't compile. You will use
var list = new ArrayList<String>();
instead. There is no benefit from the diamond syntax in the presence of var.
17
u/_INTER_ Mar 09 '16
How will this deal with code like:
I always wondered why they've choosen to abbreviate the RHS instead of the LHS (in Java 7 I think). Clear me up please.