March 08, 2004 Re: foreach ( foreach <- D ) { change syntax } | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeroen van Bemmel | > But that's just my point: they are not completely parallel. 'declaration-list' can only be 1 variable in foreach, and 'expression' denotes neither a termination conditional expression nor the expression calculated for the next loop. The syntax should be restricted to (as it is currently implemented):
>
> foreach( <type-name> <variable> ; <expression> )
>
> So all of the following are wrong (these could be test cases...):
>
> foreach( int a,b; as ) { ... } // syntax error
> foreach( int a; 1 ) { ... } // semantic error: '1' is not a
> collection type
Sorry, this was before I actually read the manual... It seems that for specific collections (the ones supporting indices) you can use at most 2 variables, 1 receiving the index and the other the value / reference of the items in the collection.
At first sight this feature hurts my eyes while reading about it. Too many exceptional cases for my taste
As long as we're doing things implicitly, why not have something like this:
foreach<c> : "a_string" {
printf( "%d: '%c'"), c.index. c );
}
where 'c.index' is defined for collections supporting indices (and could be computed for the ones that don't)
After all, 'foreach' could be considered a template where all that's missing is the name of the iteration variable...
|
Copyright © 1999-2021 by the D Language Foundation