Something like ADL from C++?
Timon Gehr
timon.gehr at gmx.ch
Wed Dec 4 02:32:04 UTC 2024
On 12/3/24 23:16, Walter Bright wrote:
> On 12/3/2024 7:17 AM, Max Samukha wrote:
>> // this is needed because we still cannot express "local to static
>> foreach".
>
> That's because static foreach will become fairly useless if it
> introduced a scope. (Same as for static if.) However, you can still add
> { } to make a scope:
>
> ```
> { static foreach(...) { } }
> ```
> Or:
> ```
> static foreach (...) {{ }}
> ```
It does introduce a scope for the loop variables, there is just no way
to add your own temporary variables to that scope.
I had this implemented as the `__local` storage class, but I did not
include it in the DIP proposal to avoid bikeshedding over syntax
delaying the entire `static foreach` implementation:
https://github.com/dlang/dmd/commit/bac3426f9881e4a591df229423475efc6c3e0918
static foreach(i;0..10){
__local enum tmp = i*i;
// use tmp here
} // no redefinition error
There was also `__previous`, that allowed to access the `__local`
variables from the previous iteration, allowing more imperative-style code:
https://github.com/dlang/dmd/commit/6c2f4b9c41cef5895efd3c1161920a47ed0b6464
static foreach(i;0..100){
enum next_i = i+1;
static if(i){
static assert(__previous.i == i-1);
static assert(__previous.next_i == i);
}
}
It's a few lines of code to make that work though, the storage class
exists, it's just not accessible to user code.
People have been using mixin templates to tuck away their
loop-body-local variables ever since, due to the way scoping works for
mixin templates (if you mix in the same mixin template multiple times
unnamed into the same scope, the declarations in them do not conflict,
but may become inaccessible from outside the mixin template instance.)
This is what that comment was on.
More information about the Digitalmars-d
mailing list