auto: useful, annoying or bad practice?

Chris wendlec at tcd.ie
Mon May 21 11:57:31 UTC 2018


On Sunday, 20 May 2018 at 23:01:39 UTC, Charles Hixson wrote:
> auto has its uses, but it's wildly overused, especially in 
> library code and documentation, and really, really, *really* 
> much so in documentation examples.
>
>

A lot of functions in `std.algorithm` are actually quite clear 
about it, e.g. `splitter`:

"auto splitter(alias pred = "a == b", Range, Separator)(Range r, 
Separator s)
...
Returns:
An input range of the subranges of elements between separators. 
If r is a forward range or bidirectional range, the returned 
range will be likewise. When a range is used a separator, 
bidirectionality isn't possible."

So it obviously returns a range which you can process as a range. 
No need to know about the exact type (which may change). All you 
need to know is that it adheres to a minimal range interface 
(r.empty, r.front, r.popFront). Without `auto`, it'd be a solid 
mess and generics would become more or less useless.

Even Java is moving into this direction and it makes sense for a 
lot of day to day issues, programmes will increasingly demand it. 
Code like the following is much more elegant than using the ould 
for loop:

JSONObject jitems = new JSONObject();
data.entrySet()
           .forEach(i -> { jitems.put(i.getKey(), i.getValue()); 
});


More information about the Digitalmars-d mailing list