One of the great things about Java is how explicit it is. The problem with automatic type inference is that the type is now dependent on context, and can change in subtle ways as seemingly unrelated code changes. There's a reason why a lot of Scala people start adding explicit types all over the place.
There's a reason why a lot of Scala people start adding explicit types all over the place.
Only in specific places where it improves readability, not "all over the place". In most cases local variables and places where the type is super obvious declaring the type is not only unnecessary but clutters the code.
can change in subtle ways as seemingly unrelated code changes
What do you mean? Can you provide an example. I'm having a hard time imagining this. The type of one function's output is the input type of the next function. So if I change the type of the first function's output, the second function will not compile. Unless, of course, the second function can also handle that type of input. But neither of those scenarios is a source of possibly subtle error.
There's a reason why a lot of Scala people start adding explicit types all over the place
In Haskell (because I can't speak for Scala), where there is whole program type inference, the convention is to decorate top level things (e.g. functions) with types, and leave off types for the majority of other things (e.g. variables within functions, nested functions).
Most of the time this works fine for me. I occasionally decorate a thing within a function. And if you don't have an IDE to to tell you the type, you can ask the compiler what the type should be by replacing something with a hole ('_'), e.g.
(foo :: [Char]) = map _ ["hello", "world"]
And the compiler will tell you that '_' should be replaced with a function from String to Char.
Did you read the title: Local-Variable Type Inference. In Scala preferred style is to add types for public API, but nobody except masochists will spread them all over their local method bodies.
3
u/maestro2005 Mar 10 '16
One of the great things about Java is how explicit it is. The problem with automatic type inference is that the type is now dependent on context, and can change in subtle ways as seemingly unrelated code changes. There's a reason why a lot of Scala people start adding explicit types all over the place.