DIP 1010--Static foreach--Formal Review

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Wed Jul 12 02:55:45 PDT 2017


On Tuesday, 11 July 2017 at 23:50:26 UTC, Steven Schveighoffer 
wrote:
> On 7/11/17 7:21 PM, H. S. Teoh via Digitalmars-d wrote:
>> On Tue, Jul 11, 2017 at 07:18:51PM -0400, Steven Schveighoffer 
>> via Digitalmars-d wrote:
>> [...]
>>> 3. The only controversial part I see is that `break` doesn't 
>>> break
>>> from the foreach loop. While I agree with the reasoning, and 
>>> support
>>> that concept, the truth is we currently have a "poor man's" 
>>> static
>>> foreach using a foreach over a tuple, and that DOES break 
>>> from the
>>> loop.
>> 
>> This is a false impression.  It actually does not break from 
>> the loop,
>> but inserts a break in the generated code, and continues to 
>> unroll the
>> rest of the loop.  It's only at codegen that the subsequent 
>> iterations
>> are detected as dead code and elided. See:
>> 
>> 	https://wiki.dlang.org/User:Quickfur/Compile-time_vs._compile-time#.22static.22_foreach_does_NOT_interpret_break_and_continue
>
> Yes, I know that it still generates all the code, but the break 
> is still interpreted as breaking out of the loop. Timon's 
> proposal says it "does not interact with break", so I interpret 
> that as meaning it should break out of whatever construct is 
> surrounding the loop, not the loop itself.
>
> Currently this:
>
> foreach(j; 0 .. 2)
> foreach(i; AliasSeq!(0, 1))
> {
>     writeln(i);
>     static if(i == 0)
>       break;
> }
>
> will print
> 0
> 0
>
> Whereas with my understanding, this:
>
> foreach(j; 0 .. 2)
> static foreach(i; 0 .. 2)
> {
>     writeln(i);
>     static if(i == 0)
>        break;
> }
>
> would print
> 0
>
> This seems too confusing.
>
> -Steve

break inside a case inside a static foreach inside a switch is an 
interesting case for this sort of reasoning


More information about the Digitalmars-d mailing list