What is the correct use of auto?

Sean Reque seanthenewt at yahoo.com
Fri Apr 11 00:01:46 PDT 2008


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?

auto is also a very handy syntactic sugar when the type of the object you are declaring is very long. In such cases, its often easier to read the auto keyword and infer what the object is by its name and how it is used, rather than by reading its type. For instance, instead of writing out a function pointer or delegate with a long signature, you could declare it as auto and save yourself and future code readers some time if you both understand that what you have is a delegate. 



More information about the Digitalmars-d mailing list