D's auto keyword

Lutger lutger.blijdestijn at gmail.com
Wed Jan 13 11:22:02 PST 2010


On 01/13/2010 05:49 PM, 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)?

A lot:

auto foo = bar(2);
auto baz = grok(foo);

No types to be seen here (aside from the literal 2) so you can:
1. rename the type whatever bar() returns without ripple effects through 
this code
2. substitute the type itself for another type that obeys the same 
structural interface.

In a way, it's like not using magic numbers only applied to types :) The 
benefits are not limited too but more pronounced in templated code.


> 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.

Depends on how this is used I think, see also below.


> 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
>

All ML derived languages do this much more extensively and they even 
don't need an auto keyword for it. C# has var and VB.NET has Dim which 
mean the same thing. The nice thing with those languages is that if you 
code in Visual Studio, you only have to hover above the variabele and 
the type pops up. Most IDE's have this btw, including descent, so that 
takes away the concern of having to remember the flow of types inferred.



More information about the Digitalmars-d mailing list