D's auto keyword

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Jan 13 10:04:31 PST 2010


Justin Johansson wrote:
> Happy New Year 2010 Everybody.
> 
> Having resumed C++ nationality for the last few months, I kind of miss D's auto keyword.
> 
> I am wondering, though, from an OO/polymorphism perspective, and UML and sound software engineering perspective as well, what does D's auto keyword buy you except (perhaps) laziness (in keystrokes)?
> 
> Sure the auto variable decl allows the declared var to take on the static type (i.e. as inferred by the compiler), but the programmer still has to know (in subsequent method invocations applied to the auto var) just what methods are valid for the statically inferred var type being the subject of the auto decl.
> 
> In some ways, as I said above, I miss "D auto" in C++; but then again, when I explicitly write the exact same type as the function return signature says, I feel more in control of my software design.
> 
> In an ideal world, which in of course such utopia does not really exist,  a pure OO model may well be that of single inheritance, and therefore all methods would, or could, be forced into a base class and hence, for object/polymorphic types D'auto keyword would not prove much advantage.
> 
> (Pray, let's not get into fragile base class discussions.)
> 
> At the end of the day, I'm not sure if D's auto keyword really helps to make my code more readable to others (alla programming-in-the-large) or if it just helps me with typing shortcuts (alla programming-in-the-small).
> 
> btw. 20 years ago I thought the Forth language was fantastic.  Then later I learned the difference between programming-in-the-small and programming-in-the-large.
> 
> Of course, Forth still hold fond memories for me .. but today I'd still rather stick to C++.
> 
> In writing this NG post, I was wondering about a subject line like "what's the best thing about D", but then my love/hate relationship with D's auto keyword really got me.
> 
> btw. Do any other languages have an "auto" var idiom?  I don't remember Scala having such (and it's really modern), though perhaps my memory lapses.
> 
> Cheers again,
> 
> Justin Johansson

"auto" is huge and has a lot to do with programming-at-large. Explicit 
types are to generic code as bad as hardcoded magic numbers to regular code.

Andrei



More information about the Digitalmars-d mailing list