D's auto keyword

Nick Sabalausky a at a.a
Wed Jan 13 13:15:02 PST 2010


"Justin Johansson" <no at spam.com> wrote in message 
news:hiktil$20ai$1 at digitalmars.com...
> 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)?
>

It makes it possible to have one variable's type match that of another 
variable without having to update as many places when you want to change. 
Ex:

---------------
class Foo {
    wchar x;
    void bar() {
        wchar[] y = x ~ "stuff";
        wchar[] z = y ~ x;
    }
}
---------------

Now if I decide I want 'x' to be a 'char', I have to remember to change 
'wchar'->'char' in all three declarations. But if 'y' and 'z' are declared 
with auto, all I have to do is change the declaration of 'x'. You could also 
get the same effect with typeof(), but that's more verbose and less DRY.





More information about the Digitalmars-d mailing list