www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - why foreach() don't use 'auto'?

reply "ketmar" <ketmar ketmar.no-ip.org> writes:
why foreach() using the current syntax instead of clearer one:
foreach (auto v; smth)?

i think that adding 'auto' will make foreach() less confusing, as
it clearly points that 'v' is a local variable for foreach()
scope. and i think that it will be in line with other forms such
as foreach(immutable v; smth), etc.

current foreach() syntax (without auto) still sometimes confusing
me and forcing to believe that i should either put 'auto' inside
foreach() or somehow declare 'v' before foreach().
Mar 27 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Thursday, 27 March 2014 at 09:19:20 UTC, ketmar wrote:
 why foreach() using the current syntax instead of clearer one:
 foreach (auto v; smth)?

 i think that adding 'auto' will make foreach() less confusing, 
 as
 it clearly points that 'v' is a local variable for foreach()
 scope. and i think that it will be in line with other forms such
 as foreach(immutable v; smth), etc.

 current foreach() syntax (without auto) still sometimes 
 confusing
 me and forcing to believe that i should either put 'auto' inside
 foreach() or somehow declare 'v' before foreach().
You can use `auto` in foreach, as well as any explicit type. It can be omitted because it is unambiguous.
Mar 27 2014
parent reply "ketmar" <ketmar ketmar.no-ip.org> writes:
 You can use `auto` in foreach, as well as any explicit type. It 
 can be omitted because it is unambiguous.
no, i can't. at least in current GDC. import std.stdio, std.string; void main () { foreach (auto val; [0,1,2,3]) writeln(val); } error: basic type expected, not auto
Mar 27 2014
next sibling parent reply "ketmar" <ketmar ketmar.no-ip.org> writes:
 error: basic type expected, not auto
ah grok it. that's cause foreach() is just a syntactic sugar for call with delegate arg, and we can't use 'auto' in arglist. it's perfectly logical from the point of compiler writer, but still forces language user to remember that foreach() is a very special beast. i think that just ignoring 'auto' in foreach() parser will not harm anyone and will bring 'normal user logic' back. but i'm relatively new to D, so i need some discussion before entering request to bugzilla: maybe this is a very old problem that was talked to death?
Mar 28 2014
parent reply Matej Nanut <matejnanut gmail.com> writes:
I was sure adding auto would work. I'm still sure it does in some
cases, but can't try right now.

I guess it depends on the type of foreach?

If I understand foreach correctly, it uses either opApply() or the
range static interface to do its job, and with a range interface
"auto" does actually work?
Mar 28 2014
parent "ketmar" <ketmar ketmar.no-ip.org> writes:
 If I understand foreach correctly, it uses either opApply() or 
 the
 range static interface to do its job, and with a range interface
 "auto" does actually work?
parser doesn't accept 'auto' in foreach(). i made a patch for it, but not sure if it will be accepted, as there is no good tests and no language spec fix.
Mar 28 2014
prev sibling parent reply "w0rp" <devw0rp gmail.com> writes:
On Thursday, 27 March 2014 at 10:30:59 UTC, ketmar wrote:
 You can use `auto` in foreach, as well as any explicit type. 
 It can be omitted because it is unambiguous.
no, i can't. at least in current GDC. import std.stdio, std.string; void main () { foreach (auto val; [0,1,2,3]) writeln(val); } error: basic type expected, not auto
I reckon that is a bug, and it should be legal. Perhaps someone else will know more.
Mar 28 2014
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 03/28/2014 01:58 PM, w0rp wrote:
 On Thursday, 27 March 2014 at 10:30:59 UTC, ketmar wrote:
 You can use `auto` in foreach, as well as any explicit type. It can
 be omitted because it is unambiguous.
no, i can't. at least in current GDC. import std.stdio, std.string; void main () { foreach (auto val; [0,1,2,3]) writeln(val); } error: basic type expected, not auto
I reckon that is a bug, and it should be legal. Perhaps someone else will know more.
It is documented that code like the above is illegal: http://dlang.org/statement.html#ForeachType
Mar 28 2014
parent reply "ketmar" <ketmar ketmar.no-ip.org> writes:
 It is documented that code like the above is illegal:
yes, it is. but i think that 'auto' is a perfect fit there. it shouldn't do anything at all, but it is the convient way to mark variable 'locality'. it will not break any of the existing code yet will bring foreach() in line with 'every variable must be explicitly declared with some type' principle.
Mar 28 2014
next sibling parent "ketmar" <ketmar ketmar.no-ip.org> writes:
 it will not break any of the existing code
i mean 'optional auto'.
Mar 28 2014
prev sibling parent "w0rp" <devw0rp gmail.com> writes:
On Friday, 28 March 2014 at 23:29:19 UTC, ketmar wrote:
 It is documented that code like the above is illegal:
yes, it is. but i think that 'auto' is a perfect fit there. it shouldn't do anything at all, but it is the convient way to mark variable 'locality'. it will not break any of the existing code yet will bring foreach() in line with 'every variable must be explicitly declared with some type' principle.
Yes, I think this might be a case for a minor thing for allowing something trivial which, honestly doesn't really matter, but it's sort of a "why not?" thing.
Mar 28 2014