r/java • • Mar 09 '16

var keyword proposed for Java

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

312 comments sorted by

View all comments

Show parent comments

23

u/ElvishJerricco Mar 09 '16

Coming from Haskell, I feel there's something people often overlook when bashing type inference. In Haskell, the type inference is better than just about any other language. And yet, we still explicitly type every single top level function. It is absolutely worth the clarity to explicitly type things used outside the current scope.

But most of the time, when something is only being used locally, the type is very clear to the reader, because it's definition is right there. And even locally, type inference is only preferred to explicit typing when the inferred type is painfully obvious. For example, a = b + c, when b and c are known to be Int. There's zero added clarity by explicitly typing a. It's totally obvious what a's type is.

So yea, inference is bad when something's type wouldn't be obvious. But when it is, the lack of inference needlessly gets in the way.

9

u/kritzikratzi Mar 09 '16

this can also be supported in the IDE. there's a million possibilities, but holding alt to reveal infered types would be a simple one.

3

u/xhak Mar 10 '16

You don't always see code in an IDE. You may use your version control browsing / compare, use crucible or fisheye or any web based stuff to view the code. So you can't rely on the IDE to add the clarity.

2

u/kritzikratzi Mar 10 '16

good point

2

u/kritzikratzi Mar 10 '16

You don't always see code in an IDE. You may use your version control browsing / compare, use crucible or fisheye or any web based stuff to view the code. So you can't rely on the IDE to add the clarity.

actually i really really like your point. and it makes me wonder if their current proposal could be targeted completely differently, namely for IDEs that could let you type "var myIterator = myMap.entrySet().iterator();" and it insert the correct type after pressing enter.

-5

u/xhak Mar 10 '16

People underestimate the number of shitty java devs out there, writing the most obscure dumb code that works more by accident than anything. A great feature of java is that it enforces and encourages some verbosity that is very helpful in these situations. Undercutting these is a loss to the language's appeal in my opinion.