Double bracket "{{" for scoping static foreach is no longer part of D

Adam D Ruppe destructionator at gmail.com
Wed Dec 22 16:10:42 UTC 2021


On Wednesday, 22 December 2021 at 15:57:29 UTC, data pulverizer 
wrote:
> I noticed that the double bracket `{{` for scoping `static 
> foreach` is no longer part of D and it looks like it has been 
> replaced with 
> https://dlang.org/changelog/2.098.0.html#AliasAssign

None of these things have anything to do with each other.

static foreach is a loop over some compile time value. It is 
special because it can be used outside a function as well as 
inside it.

static foreach's body has optional {}. Its body can contain 
whatever the context of the static foreach itself is allowed to 
contain.

Meaning if it is inside a function, it can have all the things 
inside that functions can have. This happens to include the 
nested scope statement, {}. If it is outside a function, it can 
only use things that are legal outside a function, so no nested 
scope, no expressions; just other declarations.

So OUTSIDE a function, static foreach() {{ }} is illegal because 
a plain {} is illegal outside a function.

But INSIDE a function, static foreach() {{ }} is legal, but it 
isn't magic about static foreach - it is just a body with its 
optional {} present as well as a scope statement inside.

void test() {
    int a;
    { // this is a scope statement
       int b;
    }
    // a still exists here as a local var, but b's lifetime ended 
with the preceding }.


    static foreach(...)
       stuff; // the {} are optional and i left htem out

    static foreach(...) {
       stuff; // same as above but now i put in the optional {}
    }

    // now the double {} is actually:

    static foreach(...) { // optional body {} present
        { // and this is actually one of those scope statements 
from above
            int b;
        }
    }
}



The alias assign is completely different, that's unrelated to 
either of those features. It is about overwriting one declaration 
with another if you haven't accessed it yet, giving the illusion 
of mutation in a compile time alias value.


More information about the Digitalmars-d-learn mailing list