DIP 57: static foreach

Timon Gehr timon.gehr at gmx.ch
Mon Mar 10 08:38:08 PDT 2014


On 03/10/2014 02:08 PM, Dicebot wrote:
> On Sunday, 9 March 2014 at 21:31:40 UTC, Timon Gehr wrote:
>> http://wiki.dlang.org/DIP57/
>>
>> Thoughts?
>
> 1)
>
>> Additionally, CTFE is invoked on all expressions occurring in the
>> ForeachAggregate
>
> I think it can be phrased more universally "ForeachTypeList symbols must
> be evaluated as compile-time entities, if it is not possible,
> implementation-defined compilation error happens".
> ...

I don't see how this is more universal.

> 2) Saying that it does not introduce a new scope is not entirely true as
> symbols from ForeachTypeList should not be available outside of static
> foreach. You mention it later in the same block but it is important
> concept to define as we currently don't have such pseudo-scopes
 > ...

The description only says that the usual scope for foreach statements is 
not introduced.

> (do we?)
> ...

Nope.

> 3)
>> The body of the static foreach statement or static foreach declaration
>> is duplicated once for each iteration which the corresponding foreach
>> statement with an empty body would perform when executed in CTFE
>
> I don't understand the reason behind limiting static foreach to CTFE
> semantics. Simply evaluating and pasting the body for each iteration
> should be enough. It is much closer to mixin template instances in that
> regard.
> ...

I don't understand how the DIP is 'limiting static foreach to CTFE 
semantics' and/or why this is a bad thing or how your suggestion is 
different.

> This will also remove necessity to rely on shadowing rules to re-define
> ForeachTypeList symbols as at the time of pasting the body those won't
> exist anymore.
> ...

I have no idea what this means.

> 4)
>
>>  Declarations introduced in the body itself are inserted into this
>> enclosing scope
>
> Isn't "enclosing" term used only for scope-to-scope relations or it is
> applicable to any language construct? (I don't know)
> ...

There is no formal language spec. What is meant is the scope `hosting' 
the static foreach construct.

> 5)
>
>> For static foreach statements, break and continue are supported and
>> treated like for foreach statements over tuples.
>
> It is impossible as far as I understand existing semantics. Currently
> placed continue/break refer to created scope and don't stop iteration
> over remaining template argument list members. This is not applicable to
> generic foreach.
> ...

This is not 'impossible', it is trivial to implement. Is your point that 
you would prefer break and continue to affect static foreach expansion?

> 6)
>
> In "Iterating over members of a scope" example there is a strange
> Python-like colon after `static if` condition. Typo? :)
> ...

Nope. This is a language feature. See:
http://dlang.org/version.html

> 7)
>
> In "Relation to tuple foreach" stating equivalency is not correct.

I have removed the section.

> It is more of subset and even not a strict one as semantics will differ in
> some corner cases.

I think as described they would not need to.

> For example, iterating over expression list will
> create a local copy right now if `ref` is not used. I'd really want this
> to not be the case for static foreach.
> ...

I think the description is actually not detailed enough to warrant this 
critique. (In particular, it is not clear what 'ref' should do.)

I.e., I think currently the following code is ambiguous:

int y,z;
static foreach(x;Seq!(y,z)) x = 2;
// what is the value of y and z now?

> Overall provided examples seem to much my expectations but semantics
> description can be more structured and detailed.

Agreed. I will do another iteration when I can find the time. Maybe I 
will have to re-specify the behaviour of foreach though.


More information about the Digitalmars-d mailing list