digitalmars.D - why foreach() don't use 'auto'?
- ketmar (9/9) Mar 27 2014 why foreach() using the current syntax instead of clearer one:
- Dicebot (3/14) Mar 27 2014 You can use `auto` in foreach, as well as any explicit type. It
- ketmar (6/8) Mar 27 2014 no, i can't. at least in current GDC.
- ketmar (10/11) Mar 28 2014 ah grok it. that's cause foreach() is just a syntactic sugar for
- Matej Nanut (6/6) Mar 28 2014 I was sure adding auto would work. I'm still sure it does in some
- ketmar (3/7) Mar 28 2014 parser doesn't accept 'auto' in foreach(). i made a patch for it,
- w0rp (3/11) Mar 28 2014 I reckon that is a bug, and it should be legal. Perhaps someone
- Timon Gehr (3/17) Mar 28 2014 It is documented that code like the above is illegal:
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
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
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
error: basic type expected, not autoah 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
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
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
On Thursday, 27 March 2014 at 10:30:59 UTC, ketmar wrote:I reckon that is a bug, and it should be legal. Perhaps someone else will know more.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 28 2014
On 03/28/2014 01:58 PM, w0rp wrote:On Thursday, 27 March 2014 at 10:30:59 UTC, ketmar wrote:It is documented that code like the above is illegal: http://dlang.org/statement.html#ForeachTypeI reckon that is a bug, and it should be legal. Perhaps someone else will know more.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 28 2014
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
it will not break any of the existing codei mean 'optional auto'.
Mar 28 2014
On Friday, 28 March 2014 at 23:29:19 UTC, ketmar wrote: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.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