Thread overview
Static foreach internal variable
Aug 30, 2018
Andrey
Aug 30, 2018
Basile B.
Aug 30, 2018
drug
Aug 30, 2018
Andrey
August 30, 2018
Hello,
is it possible to declare an internal variable in "static foreach" and on each iteration assign something to it?
Example:
>static foreach(arg; SomeAliasSeq)
>{
>    internal = arg[0].converted;    // a shortcut for expression "arg[0].converted"
>
>    static if(internal.length == 0) { ... }
>    else static if(internal.isNull) { ... }
>    else { ... }
>}
August 30, 2018
On Thursday, 30 August 2018 at 08:19:47 UTC, Andrey wrote:
> Hello,
> is it possible to declare an internal variable in "static foreach" and on each iteration assign something to it?
> Example:
>>static foreach(arg; SomeAliasSeq)
>>{
>>    internal = arg[0].converted;    // a shortcut for expression "arg[0].converted"
>>
>>    static if(internal.length == 0) { ... }
>>    else static if(internal.isNull) { ... }
>>    else { ... }
>>}

Add a scope :

static foreach(arg; SomeAliasSeq)
{{
}}

And this should work without complaint about "internal" already existing.
August 30, 2018
30.08.2018 11:19, Andrey пишет:
> Hello,
> is it possible to declare an internal variable in "static foreach" and on each iteration assign something to it?
> Example:
>> static foreach(arg; SomeAliasSeq)
>> {
>>    internal = arg[0].converted;    // a shortcut for expression "arg[0].converted"
>>
>>    static if(internal.length == 0) { ... }
>>    else static if(internal.isNull) { ... }
>>    else { ... }
>> }
static foreach will be lowered to:
```
{
    auto internal = SomeAliasSeq[0][0].converted;

    static if(internal.length == 0) { ... }
    else static if(internal.isNull) { ... }
    else { ... }

    auto internal = SomeAliasSeq[1][0].converted;

    static if(internal.length == 0) { ... }
    else static if(internal.isNull) { ... }
    else { ... }

    ...

    auto internal = SomeAliasSeq[N][0].converted;   // N == SomeAliasSeq.length

    static if(internal.length == 0) { ... }
    else static if(internal.isNull) { ... }
    else { ... }
}
```
if you use scope it will be
```
{
    {
        auto internal = SomeAliasSeq[0][0].converted;

        static if(internal.length == 0) { ... }
        else static if(internal.isNull) { ... }
        else { ... }
    }

    {
        auto internal = SomeAliasSeq[1][0].converted;

        static if(internal.length == 0) { ... }
        else static if(internal.isNull) { ... }
        else { ... }
    }

    ...

    {
        auto internal = SomeAliasSeq[N][0].converted;   // N == SomeAliasSeq.length

        static if(internal.length == 0) { ... }
        else static if(internal.isNull) { ... }
        else { ... }
    }
}
```
every instance of internal variable will be defined in its own scope
August 30, 2018
On Thursday, 30 August 2018 at 09:49:15 UTC, drug wrote:
> 30.08.2018 11:19, Andrey пишет:

Thanks everybody. Works!