D's auto keyword

dsimcha dsimcha at yahoo.com
Wed Jan 13 09:19:05 PST 2010


== Quote from Justin Johansson (no at spam.com)'s article
> 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

One underappreciated thing auto gives is DRY for types.  It makes it easier to
change the type of some object in the place where it's initially decided, because
those changes will automagically be propagated to everything that uses that
object, as long as the new type supports the same compile-time interface as the
old type.



More information about the Digitalmars-d mailing list