<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2013/5/16 TommiT <span dir="ltr"><<a href="mailto:tommitissari@hotmail.com" target="_blank">tommitissari@hotmail.com</a>></span><br><blockquote class="gmail_quote" style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
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:<br>
<br>
const string[100] int__str;<br>
const int[string] str__int;<br>
<br>
for (int i = 0; i < 100; ++i)<br>
{<br>
    auto str = to!string(i);<br>
    int__str[i] = str; // ERROR: Can't modify const<br>
    str__int[str] = i; // ERROR: Can't modify const<br>
}<br>
<br>
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:<br>
<br>
const string[100] int__str = {<br>
    string[100] tmp;<br>
    // ... init tmp ...<br>
    return tmp;<br>
}();<br>
<br>
...But I can't see any easy solution for initializing two or more const variables at the same time.<br>
<br>
Here's my proposal: "initialization scope". You'd use it like this:<br>
<br>
initialization {<br>
    const string[100] int__str;<br>
    const int[string] str__int;<br>
<br>
    for (int i = 0; i < 100; ++i)<br>
    {<br>
        auto str = to!string(i);<br>
        int__str[i] = str; // OK<br>
        str__int[str] = i; // OK<br>
    }<br>
}<br>
<br>
string s = int__str[42]; // OK<br>
int__str[42] = "43" // ERROR: Can't modify const<br>
<br>
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.<br>

</blockquote></div><br></div><div class="gmail_extra">Pure delegate and implicit conversion for unique expression should work.</div><div class="gmail_extra"><br></div><div class="gmail_extra">import std.conv;</div><div class="gmail_extra">
<div class="gmail_extra">void main()</div><div class="gmail_extra">{</div><div class="gmail_extra">    immutable string[100] int__str = () pure {</div><div class="gmail_extra">        string[100] tmp;</div><div class="gmail_extra">
        debug printf("&tmp[0] = %p\n", &tmp[0]);        // [1]</div><div class="gmail_extra">        for (int i = 0; i < 100; ++i)</div><div class="gmail_extra">        {</div><div class="gmail_extra">
            auto str = to!string(i);</div><div class="gmail_extra">            // BUG: pure function '__lambda1' cannot call impure function 'to'</div><div class="gmail_extra">            tmp[i] = str;</div>
<div class="gmail_extra">        }</div><div class="gmail_extra">        return tmp; // BUG: NRVO doesn't work</div><div class="gmail_extra">    }();</div><div class="gmail_extra">    debug printf("&int__str[0] = %p\n", &int__str[0]);  // [2]</div>
<div class="gmail_extra">}</div><div class="gmail_extra"><br></div><div class="gmail_extra">Compiler bug: Currently [1] and [2] does not print same address.</div><div class="gmail_extra">Phobos bug: Currently <a href="http://std.conv.to">std.conv.to</a> is not pure.</div>
<div><br></div><div>Kenji Hara</div></div></div>