MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/java/comments/49prnu/var_keyword_proposed_for_java/d0uit6t/?context=3
r/java • u/adila01 • Mar 09 '16
312 comments sorted by
View all comments
-3
My argument against it is code like this:
var c = Class.forName("com.foo.Bar")
where you don't know the type of 'c' unless you know the type the method returns (which is not obvious by looking at it)
9 u/ElvishJerricco Mar 09 '16 That expression would actually return Class<?>. So you would have to cast it to Class<Bar> with or without the var keyword. Doing this solves your problem.var c = (Class<Bar>) Class.forName("com.foo.Bar"); 3 u/vytah Mar 10 '16 Wouldn't Class<Bar> mean that Bar is in your classpath, and therefore forName would be unnecessary, since you could just write Bar.class? 2 u/ElvishJerricco Mar 10 '16 True. And in that case, var would be appreciated, since the type of Bar.class would be super obvious.
9
That expression would actually return Class<?>. So you would have to cast it to Class<Bar> with or without the var keyword. Doing this solves your problem.
Class<?>
Class<Bar>
var
var c = (Class<Bar>) Class.forName("com.foo.Bar");
3 u/vytah Mar 10 '16 Wouldn't Class<Bar> mean that Bar is in your classpath, and therefore forName would be unnecessary, since you could just write Bar.class? 2 u/ElvishJerricco Mar 10 '16 True. And in that case, var would be appreciated, since the type of Bar.class would be super obvious.
3
Wouldn't Class<Bar> mean that Bar is in your classpath, and therefore forName would be unnecessary, since you could just write Bar.class?
Bar
forName
Bar.class
2 u/ElvishJerricco Mar 10 '16 True. And in that case, var would be appreciated, since the type of Bar.class would be super obvious.
2
True. And in that case, var would be appreciated, since the type of Bar.class would be super obvious.
-3
u/slartybartfast_ Mar 09 '16
My argument against it is code like this:
where you don't know the type of 'c' unless you know the type the method returns (which is not obvious by looking at it)