New feature proposal: "initialization scope"

dennis luehring dl.soluz at gmx.net
Thu May 16 01:06:47 PDT 2013


Am 16.05.2013 10:01, schrieb deadalnix:
> On Thursday, 16 May 2013 at 07:53:08 UTC, TommiT wrote:
>> I'd like to make it easier to initialize function local
>> immutable/const data. Here's the type of problem I'd like to
>> alleviate:
>>
>> const string[100] int__str;
>> const int[string] str__int;
>>
>> for (int i = 0; i < 100; ++i)
>> {
>>     auto str = to!string(i);
>>     int__str[i] = str; // ERROR: Can't modify const
>>     str__int[str] = i; // ERROR: Can't modify const
>> }
>>
>> In short, I want to initialize two different const variables at
>> once (in the same loop or other block). 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;
>> }();
>>
>
> This is easy to work around that using a mutable data, construct
> it and then copy it to a const one.
>
> You my argue that this is slower, and I answer you : maybe. Can
> you show what does GDC or LDC output doing so ?

or even better - make them members of an helper class, with lookup 
methods and initialzation in ctor and put that into an const ref



More information about the Digitalmars-d mailing list