What is the correct use of auto?

Unknown W. Brackets unknown at simplemachines.org
Thu Apr 10 20:28:22 PDT 2008


A great example is when using a library.  Instead of using a "void*" or 
something like that, you'd use an auto.

Example:

auto valueType = library.getSomething();

auto souffle = library.makeSomethingSouffle(valueType);
library.somethingElse(souffle);

Another use is when a particular section of your code doesn't care about 
the type, and you're just paper-pushing.  This way, even if you need to 
change the type later, you don't have to revisit the code (just 
recompile it.)

It's also handy for this:

auto abc = new com.example.somethinglong.modulename.Package();

Although mostly you would use an alias for that anyway.

And that's not even mentioning templates, where it's very very useful.

-[Unknown]


Hans W. Uhlig wrote:
> I have been reading through the specification and playing with D more 
> and more, what is the purpose of auto. I can understand in languages 
> with scalar types handling datatypes on assignment but on a strictly 
> typed language like C or D when would auto(as a variable declaration) 
> provide more useful functionality then it removes from readability.
> 
> When would this be useful rather then simply specifying the type?



More information about the Digitalmars-d mailing list