New feature proposal: "initialization scope"

Timothee Cour thelastmammoth at gmail.com
Thu May 16 01:01:31 PDT 2013


this works:


import std.stdio;
import std.conv;
const string[100] int__str;
const int[string] str__int;

static this(){
    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
    }
}



On Thu, May 16, 2013 at 12:57 AM, dennis luehring <dl.soluz at gmx.net> wrote:

> doesn't that break the constness for other threads usage?
>
> (not in your example - but in any other using threads, const globals and
> your proposed "initialization")
>
> Am 16.05.2013 09:53, schrieb TommiT:
>
>  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;
>> }();
>>
>> ...But I can't see any easy solution for initializing two or more
>> const variables at the same time.
>>
>> Here's my proposal: "initialization scope". You'd use it like
>> this:
>>
>> initialization {
>>       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; // OK
>>           str__int[str] = i; // OK
>>       }
>> }
>>
>> string s = int__str[42]; // OK
>> int__str[42] = "43" // ERROR: Can't modify const
>>
>> As you can see, 'initialization scope' would be a scope that is
>> not a lexical scope (like static if), it merely makes all const
>> and immutable variables created in that scope modifiable inside
>> that scope but not after it.
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130516/889052a8/attachment-0001.html>


More information about the Digitalmars-d mailing list