foreach and element type inference

Jonny via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 23 20:58:45 PST 2015


On Friday, 20 November 2015 at 19:37:09 UTC, Nicolas F. wrote:
"
Allowing "auto" here, as somebody pointed out, would lead to two 
styles which may be confusing as one might mistake "foreach (e" 
to be different from "foreach (auto e", and even worse, might be 
led to believe that "e" was declared earlier in the former case.
"

So, doesn't that logically suggest that auto e should be kept and 
e should disregarded or given the error you suggested?

foreach(e; x) looks like e is an unqualified type.
foreach(auto e; x) looks like e has type "auto", which makes 
sense.

I think most C programmers looking at D without any previous D 
experiences would think using auto made more sense.

Else, why not allow stuff like

e = 1;

instead of

auto e = 1;

?



More information about the Digitalmars-d mailing list