www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - to auto or not to auto ( in foreach )

reply dom <dschoerk gmx.at> writes:
foreach(auto v; msg)
   writeln(v);

gives an error that a basic type is expected

foreach(v; msg)
   writeln(v);

works

.. but why?
Jul 16 2016
next sibling parent Dennis Ritchie <dennis.ritchie mail.ru> writes:
On Saturday, 16 July 2016 at 14:00:56 UTC, dom wrote:
 foreach(auto v; msg)
   writeln(v);

 gives an error that a basic type is expected

 foreach(v; msg)
   writeln(v);

 works

 .. but why?
`Note: The ForeachTypeAttribute is implicit, and when a type is not specified, it is inferred. In that case, auto is implied, and it is not necessary (and actually forbidden) to use it.` http://dlang.org/spec/statement.html#ForeachStatement
Jul 16 2016
prev sibling next sibling parent reply cym13 <cpicard openmailbox.org> writes:
On Saturday, 16 July 2016 at 14:00:56 UTC, dom wrote:
 foreach(auto v; msg)
   writeln(v);

 gives an error that a basic type is expected

 foreach(v; msg)
   writeln(v);

 works

 .. but why?
Arbitrary limitation. If you want to say how surprising and uselessly limiting it is wait at the end of the line. It's not actually a problem in practice because you just have not to put it but it is part of those frustrating little edge cases with no reason to be.
Jul 16 2016
parent reply Seb <seb wilzba.ch> writes:
On Saturday, 16 July 2016 at 14:11:34 UTC, cym13 wrote:
 On Saturday, 16 July 2016 at 14:00:56 UTC, dom wrote:
 foreach(auto v; msg)
   writeln(v);

 gives an error that a basic type is expected

 foreach(v; msg)
   writeln(v);

 works

 .. but why?
Arbitrary limitation. If you want to say how surprising and uselessly limiting it is wait at the end of the line. It's not actually a problem in practice because you just have not to put it but it is part of those frustrating little edge cases with no reason to be.
It's not arbitrary. It keeps the language simple and easy to read. After all the entire auto keyword is just there, because the compiler needs a keyword and in loops it's clearly defined what the type will be. You don't complain that `int int` is forbidden, or do you? I guess you are frustrated because you are used to this pattern from other, inferior languages. I bet you will soon start to appreciate the syntactic sugar that D provides.
Jul 16 2016
next sibling parent Seb <seb wilzba.ch> writes:
On Saturday, 16 July 2016 at 20:00:39 UTC, Seb wrote:
 On Saturday, 16 July 2016 at 14:11:34 UTC, cym13 wrote:
 [...]
It's not arbitrary. It keeps the language simple and easy to read. After all the entire auto keyword is just there, because the compiler needs a keyword and in loops it's clearly defined what the type will be. You don't complain that `int int` is forbidden, or do you? I guess you are frustrated because you are used to this pattern from other, inferior languages. I bet you will soon start to appreciate the syntactic sugar that D provides.
However I agree that the error message should be more informative. Please open a issue for this ;-)
Jul 16 2016
prev sibling parent reply cym13 <cpicard openmailbox.org> writes:
On Saturday, 16 July 2016 at 20:00:39 UTC, Seb wrote:
 On Saturday, 16 July 2016 at 14:11:34 UTC, cym13 wrote:
 On Saturday, 16 July 2016 at 14:00:56 UTC, dom wrote:
 foreach(auto v; msg)
   writeln(v);

 gives an error that a basic type is expected

 foreach(v; msg)
   writeln(v);

 works

 .. but why?
Arbitrary limitation. If you want to say how surprising and uselessly limiting it is wait at the end of the line. It's not actually a problem in practice because you just have not to put it but it is part of those frustrating little edge cases with no reason to be.
It's not arbitrary. It keeps the language simple and easy to read. After all the entire auto keyword is just there, because the compiler needs a keyword and in loops it's clearly defined what the type will be. You don't complain that `int int` is forbidden, or do you? I guess you are frustrated because you are used to this pattern from other, inferior languages. I bet you will soon start to appreciate the syntactic sugar that D provides.
int int is a completely different issue, and that has nothing to do with what you suppose my experience is from other languages. If anything I come mainly from python so I find not having to write the type a pretty thing. However auto should be allowed here. You are defining a variable and the fact that it's in a foreach shouldn't be of any importance. The language should enforce orthogonality of orthogonal things, not break it. A variable definition in a foreach should be allowed not because I like to be pedantic but because it's allowed everywhere else. That also stands for the principle of least surprise.
Jul 16 2016
next sibling parent cym13 <cpicard openmailbox.org> writes:
On Saturday, 16 July 2016 at 21:39:42 UTC, cym13 wrote:
 A variable definition in a foreach
-> A variable definition *with auto* in a foreach
Jul 16 2016
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Saturday, 16 July 2016 at 21:39:42 UTC, cym13 wrote:
 However auto should be allowed here. You are defining a 
 variable and the fact that it's in a foreach shouldn't be of 
 any importance. The language should enforce orthogonality of 
 orthogonal things, not break it. A variable definition in a 
 foreach should be allowed not because I like to be pedantic but 
 because it's allowed everywhere else. That also stands for the 
 principle of least surprise.
actually, `foreach (v; rng)` looks like `foreach` is *reusing* *existing* *variable*. most of the time you can put `immutable` or something like that there to note that it is not reusing (purely cosmetical thing), but sometimes you cannot, and then `auto` is perfect candidate... but it is not allowed. (sigh)
Jul 16 2016
next sibling parent reply pineapple <meapineapple gmail.com> writes:
On Saturday, 16 July 2016 at 22:05:49 UTC, ketmar wrote:
 actually, `foreach (v; rng)` looks like `foreach` is *reusing* 
 *existing* *variable*. most of the time you can put `immutable` 
 or something like that there to note that it is not reusing 
 (purely cosmetical thing), but sometimes you cannot, and then 
 `auto` is perfect candidate... but it is not allowed. (sigh)
Chipping in my agreement. foreach(x; y) makes as much syntactic sense as for(x = 0; x < y; x++) where x was not previously defined. One does not expect something that does not look like every other variable definition in the language to be defining a new variable.
Jul 16 2016
next sibling parent reply pineapple <meapineapple gmail.com> writes:
On Sunday, 17 July 2016 at 01:57:21 UTC, pineapple wrote:
 On Saturday, 16 July 2016 at 22:05:49 UTC, ketmar wrote:
 actually, `foreach (v; rng)` looks like `foreach` is *reusing* 
 *existing* *variable*. most of the time you can put 
 `immutable` or something like that there to note that it is 
 not reusing (purely cosmetical thing), but sometimes you 
 cannot, and then `auto` is perfect candidate... but it is not 
 allowed. (sigh)
Chipping in my agreement. foreach(x; y) makes as much syntactic sense as for(x = 0; x < y; x++) where x was not previously defined. One does not expect something that does not look like every other variable definition in the language to be defining a new variable.
Furthermore, if foreach(int x; y) is legal then why isn't foreach(auto x; y)?
Jul 16 2016
parent reply Lobelia Noakes <LobeliaNoakes armyspy.com> writes:
On Sunday, 17 July 2016 at 01:58:59 UTC, pineapple wrote:
 On Sunday, 17 July 2016 at 01:57:21 UTC, pineapple wrote:
 On Saturday, 16 July 2016 at 22:05:49 UTC, ketmar wrote:
 actually, `foreach (v; rng)` looks like `foreach` is 
 *reusing* *existing* *variable*. most of the time you can put 
 `immutable` or something like that there to note that it is 
 not reusing (purely cosmetical thing), but sometimes you 
 cannot, and then `auto` is perfect candidate... but it is not 
 allowed. (sigh)
Chipping in my agreement. foreach(x; y) makes as much syntactic sense as for(x = 0; x < y; x++) where x was not previously defined. One does not expect something that does not look like every other variable definition in the language to be defining a new variable.
Furthermore, if foreach(int x; y) is legal then why isn't foreach(auto x; y)?
By the way there's an error in the grammar: ForeachTypeAttribute: ref TypeCtor But BasicType also already includes TypeCtor. So a TypeCtor in a foreach typelist is ? well hard to say, part of basic type or part of ForeachTypeAttribute ?
Jul 20 2016
parent Lobelia Noakes <LobeliaNoakes armyspy.com> writes:
On Wednesday, 20 July 2016 at 15:40:16 UTC, Lobelia Noakes wrote:
 On Sunday, 17 July 2016 at 01:58:59 UTC, pineapple wrote:
 On Sunday, 17 July 2016 at 01:57:21 UTC, pineapple wrote:
 On Saturday, 16 July 2016 at 22:05:49 UTC, ketmar wrote:
 actually, `foreach (v; rng)` looks like `foreach` is 
 *reusing* *existing* *variable*. most of the time you can 
 put `immutable` or something like that there to note that it 
 is not reusing (purely cosmetical thing), but sometimes you 
 cannot, and then `auto` is perfect candidate... but it is 
 not allowed. (sigh)
Chipping in my agreement. foreach(x; y) makes as much syntactic sense as for(x = 0; x < y; x++) where x was not previously defined. One does not expect something that does not look like every other variable definition in the language to be defining a new variable.
Furthermore, if foreach(int x; y) is legal then why isn't foreach(auto x; y)?
By the way there's an error in the grammar: ForeachTypeAttribute: ref TypeCtor But BasicType also already includes TypeCtor. So a TypeCtor in a foreach typelist is ? well hard to say, part of basic type or part of ForeachTypeAttribute ?
It's a minor issue BTW. I think that everybody that would write a D parser will skip parsing of TypeCtor in ForeachTypeAttribute and rather consider them as part of the type. I'm not sure if it can be completly removed from ForeachTypeAttribute...Does anyone know ?
Jul 20 2016
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 17 July 2016 at 01:57:21 UTC, pineapple wrote:
 Chipping in my agreement. foreach(x; y) makes as much syntactic 
 sense as for(x = 0; x < y; x++) where x was not previously 
 defined. One does not expect something that does not look like 
 every other variable definition in the language to be defining 
 a new variable.
(x) => x; // defines a new variable foreach isn't alone.
Jul 16 2016
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Sunday, 17 July 2016 at 02:04:50 UTC, Adam D. Ruppe wrote:
 (x) => x; // defines a new variable
 foreach isn't alone.
compiler should allow `auto` here too.
Jul 16 2016
prev sibling parent Johan Engelen <j j.nl> writes:
On Saturday, 16 July 2016 at 22:05:49 UTC, ketmar wrote:
 
 `foreach (v; rng)` looks like `foreach` is *reusing* *existing* 
 *variable*.
+1
Jul 18 2016
prev sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Saturday, 16 July 2016 at 14:00:56 UTC, dom wrote:
 .. but why?
because. i've lost that fight too.
Jul 16 2016