DIP 1010--Static foreach--Accepted

via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Jul 17 06:23:49 PDT 2017


On Monday, 17 July 2017 at 12:38:27 UTC, Steven Schveighoffer 
wrote:
> On 7/16/17 1:04 PM, Andrei Alexandrescu wrote:
>> On 7/16/17 9:10 AM, Mike Parker wrote:
>>> Congratulations to Timon Gehr. Not only was his "Static 
>>> foreach" DIP accepted, it picked up a good deal of praise 
>>> from Walter & Andrei.
>> 
>> Indeed. Kudos to Timon (and thanks Mike for driving the 
>> process). This is a well done DIP that many others could draw 
>> inspiration from. -- Andrei
>
> What is the resolution of how break statements affect static 
> foreach/foreach?
>
> i.e. this section:
>
> "As some consider this to be potentially confusing, it has been 
> suggested that break and continue directly inside static 
> foreach should instead be compiler errors and require explicit 
> labels. This DIP leaves this decision to the language authors, 
> but recommends the above semantics."
>
> -Steve

I think the only reliable way is to not use jumps (goto, break, 
continue) at all.
E.g. if you want to unroll the following loop:

foreach (x; someRange)
{
     if (x.isSpecial)
         break;

     x.writeln();
}

You would need to guard every statement/declaration:

static foreach (x; someRange)
     static if (!x.isSpecial)
         x.writeln();

Hence why, I believe that we need more powerful range-like 
algorithms for manipulating alias sequences. Though in case this 
using what's in std.meta is not much of a stretch, ultimately it 
becomes repetitive and very verbose when used more heavily and 
ultimately doesn't offer significant improvement over the code 
above:

foreach (x; Filter!(templateNot!isSpecial, aliasSeqOf!someRange))
     x.writeln();

(I'm working on a functional programming library which would 
allow to use the same functions to transform ranges, alias 
sequences and other reducible/iterable objects, which should make 
composing alias sequence transformations a bit more easy and 
scalable.)

Anyway, if you're iterating over homogeneous expression 
sequences, via DIP1010 you should be able to use std.algorithm 
and std.range functions directly, since the resulting range would 
be automatically evaluated at CT and expanded as an expression 
sequence:

static foreach (x; someRange.filter!(x => !x.isSpecial))
     x.writeln();




More information about the Digitalmars-d-announce mailing list