www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - odd syntax for foreach

reply BCS <BCS pathlink.com> writes:
from: http://www.digitalmars.com/d/statement.html#foreach

<bnf>
ForeachStatement:
	foreach (ForeachTypeList; Expression) Statement

ForeachTypeList:
	ForeachType
	ForeachType , ForeachTypeList
</bnf>

Based on this the following is syntacticly correct.

void main()
{
char[] foo;
foreach(i,j,k;foo)
{}
}

In fact it gives the error

    foreach.d(5): cannot infer type for j

which tells you nothing of much use. The following syntax looks more 
reasonable to me.

<bnf>
ForeachTypeList:
	ForeachType
	ForeachType , ForeachType
</bnf>
Aug 19 2006
parent reply Oskar Linde <olREM OVEnada.kth.se> writes:
BCS wrote:

 from: http://www.digitalmars.com/d/statement.html#foreach
 
 <bnf>
 ForeachStatement:
 foreach (ForeachTypeList; Expression) Statement
 
 ForeachTypeList:
 ForeachType
 ForeachType , ForeachTypeList
 </bnf>
 
 Based on this the following is syntacticly correct.
 
 void main()
 {
 char[] foo;
 foreach(i,j,k;foo)
 {}
 }
 
 In fact it gives the error
 
     foreach.d(5): cannot infer type for j
Which is a correct error message.
 
 which tells you nothing of much use. The following syntax looks more
 reasonable to me.
 
 <bnf>
 ForeachTypeList:
 ForeachType
 ForeachType , ForeachType
 </bnf>
Why? How would you then handle more than two variable foreach loops? foreach(i,j,k,l,m;foo) is a valid syntax given a foo with an opApply taking a 5 argument delegate. /Oskar
Aug 19 2006
parent reply BCS <foobar pathlink.net> writes:
Oskar Linde wrote:
 BCS wrote:
 from: http://www.digitalmars.com/d/statement.html#foreach

 <bnf>
 ForeachStatement:
 foreach (ForeachTypeList; Expression) Statement

 ForeachTypeList:
 ForeachType
 ForeachType , ForeachTypeList
 </bnf>

 Based on this the following is syntacticly correct.

 void main()
 {
 char[] foo;
 foreach(i,j,k;foo)
 {}
 }

 In fact it gives the error

     foreach.d(5): cannot infer type for j
Which is a correct error message.
but it tells you nothing about what is wrong.
 which tells you nothing of much use. The following syntax looks
 more reasonable to me.

 <bnf>
 ForeachTypeList:
 ForeachType
 ForeachType , ForeachType
 </bnf>
Why? How would you then handle more than two variable foreach loops? foreach(i,j,k,l,m;foo) is a valid syntax given a foo with an opApply taking a 5 argument delegate. /Oskar
I known of nowhere that says that anthing but the 1 and 2 argument forms of the opApply are even valid. I haven't tried it though. Looking at the sepc, it looks like it might be allowed, but it dosn't actualy say so. Whatever the case, the docs need a little work here and the error message gives nothing usefull other than the line number
Aug 19 2006
parent Oskar Linde <olREM OVEnada.kth.se> writes:
BCS wrote:

 Oskar Linde wrote:
 BCS wrote:
 from: http://www.digitalmars.com/d/statement.html#foreach

 <bnf>
 ForeachStatement:
 foreach (ForeachTypeList; Expression) Statement

 ForeachTypeList:
 ForeachType
 ForeachType , ForeachTypeList
 </bnf>

 Based on this the following is syntacticly correct.

 void main()
 {
 char[] foo;
 foreach(i,j,k;foo)
 {}
 }

 In fact it gives the error

     foreach.d(5): cannot infer type for j
Which is a correct error message.
but it tells you nothing about what is wrong.
It tells you exactly what was wrong to the compiler. It didn't find a way to infer the type for j. Such error messages are often better than if the compiler tries to guess what you intended.
 
 which tells you nothing of much use. The following syntax looks
 more reasonable to me.

 <bnf>
 ForeachTypeList:
 ForeachType
 ForeachType , ForeachType
 </bnf>
Why? How would you then handle more than two variable foreach loops? foreach(i,j,k,l,m;foo) is a valid syntax given a foo with an opApply taking a 5 argument delegate. /Oskar
I known of nowhere that says that anthing but the 1 and 2 argument forms of the opApply are even valid. I haven't tried it though. Looking at the sepc, it looks like it might be allowed, but it dosn't actualy say so.
It is allowed and works very well.
 
 Whatever the case, the docs need a little work here and the error message
 gives nothing usefull other than the line number
The correct file? The correct line number? And you are still complaining? ;) /Oskar
Aug 20 2006