odd syntax for foreach

BCS foobar at pathlink.net
Sat Aug 19 16:53:05 PDT 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



More information about the Digitalmars-d-bugs mailing list