Thread overview
odd syntax for foreach
Aug 19, 2006
BCS
Aug 19, 2006
Oskar Linde
Aug 19, 2006
BCS
Aug 20, 2006
Oskar Linde
August 19, 2006
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>
August 19, 2006
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
August 19, 2006
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
August 20, 2006
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