auto: useful, annoying or bad practice?

Chris wendlec at tcd.ie
Fri May 18 10:09:20 UTC 2018


On Thursday, 17 May 2018 at 11:38:13 UTC, Kagamin wrote:
>
> I prefer types spelled as it helps to understand the code. In 
> javascript I have to look around to figure out types of 
> variables and then I can understand the code. In C# I saw 
> surprising abuse like `var id = 0L;` - to think someone would 
> go to such length only to insist on type inference.
> In part this can be due to unreasonably difficult to spell 
> types like ptrdiff_t, so I try to design my code in a way that 
> it's easy to spell types.

I was working with Java 8 this week, and God did I miss `auto`. 
And now Java will get `var`[1], t'was about time, indeed! Java 8 
introduced lambdas and functional style features. For lambdas and 
streams the compiler aleady performs automatic type inference (as 
it does for generics, btw). So the next logical step is local 
type inference with `var`. I think that a functional apporach 
(stream().filter().map()...) leads almost automatically to `auto` 
(pardon the pun). Else programming becomes very cumbersome, 
because you have to keep track of the types all the way down the 
chain. That's time better spent somewhere else.

In a way Java has slowly been moving in that direction anyway, 
cf. this answer [2] that reminded me of D's `auto` return type.


[1] http://segasolutionsinc.com/2018/03/30/var-in-java10/
[2] 
https://stackoverflow.com/questions/1348199/what-is-the-difference-between-the-hashmap-and-map-objects-in-java


More information about the Digitalmars-d mailing list