static foreach (Was: Re: static switch)

Timon Gehr timon.gehr at gmx.ch
Thu Mar 6 13:36:06 PST 2014


On 03/06/2014 12:35 AM, bearophile wrote:
> Andrei Alexandrescu:
>
>> Walter and I would preapprove implementation of static foreach if and
>> only if a solid DIP comes about.
>
> Some suggestions for a static foreach DIP:
> - It should work at global scope too (I'd like with() to work at global
> scope too).

Of course. static foreach will be both a declaration and a statement.

> - The semantics of "static foreach (x; TypeTuple!(1, 2))" should not
> change, if possible (in alternative it could change a little, but
> eventually become a syntax error).

No. static foreach does not introduce a new scope. "TypeTuple"-foreach 
does. Deprecating this language feature is not in scope of the 'static 
foreach' DIP. It's separate, and I'd even argue for not doing it.

> - At first "foreach (x; TypeTuple!(1, 2))"  should give a warning, then
> a deprecation message, and then an error message that "static" is
> required. This makes iteration on type tuples visibly static.

It's not the same thing.

> - "static foreach_reverse (immutable i; 0 .. 10)" could be supported.

Good point.

> - "static foreach (immutable i; iota(1, 10, 2))" should work.
> - Please no tuple unpacking, because this foreach feature should die and
> be replaced by something more general and more correct.
> ...

foreach and static foreach should behave the same in all shared aspects. 
(Unfortunately, this statement is somewhat messy to formalize.)


More information about the Digitalmars-d mailing list