New feature proposal: "initialization scope"

Ali Çehreli acehreli at yahoo.com
Thu May 16 10:14:24 PDT 2013


On 05/16/2013 03:20 AM, bearophile wrote:
> TommiT:
>
>> If I needed to initialize only one const variable, I could use a lambda:
>>
>> const string[100] int__str = {
>>     string[100] tmp;
>>     // ... init tmp ...
>>     return tmp;
>> }();
>>
>> ...But I can't see any easy solution for initializing two or more
>> const variables at the same time.
>
> Once we have a tuple unpacking syntax, you return and assign to two
> const variables at the same time.
>
> Bye,
> bearophile

Until then, we have to define a local Tuple variable ('vars' below):

import std.conv;
import std.typecons;

void foo(size_t N)()
{
     auto init() {
         string[N] int__str;
         int[string] str__int;

         foreach (i; 0 .. N) {
             auto str = to!string(i);
             int__str[i] = str;
             str__int[str] = to!int(i);
         }

         return tuple(int__str, str__int);
     }

     const vars = init();

     const string[N] int__str = vars[0];
     const int[string] str__int = vars[1];
}

void main()
{
     foo!100();
}

Ali


More information about the Digitalmars-d mailing list