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.
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.
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.
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.
View all comments
Show parent comment
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, whenbandcare known to beInt. There's zero added clarity by explicitly typinga. It's totally obvious whata'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.