Auto keyword and when to use it

XavierAP n3minis-git at yahoo.es
Tue Aug 21 16:15:32 UTC 2018


On Monday, 20 August 2018 at 17:52:17 UTC, QueenSvetlana wrote:
>
> So I can't declare class level variables with auto, correct? 
> only local method variables?

One difference between D's auto and C#'s var or C++'s auto is 
that the latter languages allow automatically typed declarations 
only for local (method/function-scope) variables, and forbid them 
for class or struct member variables (aka fields); whereas D 
allows auto anywhere (even function/method return type! -- which 
C# and C++ allow as well but only case of anonymous 
methods/lambdas).

I'm in favor of the AAA ("Auto" Almost Always) paradigm, but as 
long as the type if obvious to a human reader. I don't favor them 
for numeric types for this reason (non obvious bitsize, 
signedness...) It's up to each programmer. Only if someone likes 
"Type x = new Type()" instead of "auto x = new Type()" I would 
say they're clearly wrong.


More information about the Digitalmars-d-learn mailing list