this works:<div><br></div><div>
<font face="Menlo">
<br>
<span style="color:#009695">import</span><span style="color:#444444"> </span><span style="color:#444444">std</span><span style="color:#444444">.</span><span style="color:#444444">stdio</span><span style="color:#444444">;</span><br>
<span style="color:#009695">import</span><span style="color:#444444"> </span><span style="color:#444444">std</span><span style="color:#444444">.</span><span style="color:#444444">conv</span><span style="color:#444444">;</span><br>
<span style="color:#009695">const</span><span style="color:#444444"> </span><span style="color:#009695">string</span><span style="color:#444444">[</span><span style="color:#f57d00">100</span><span style="color:#444444">]</span><span style="color:#444444"> </span><span style="color:#444444">int__str</span><span style="color:#444444">;</span><br>
<span style="color:#009695">const</span><span style="color:#444444"> </span><span style="color:#009695">int</span><span style="color:#444444">[</span><span style="color:#009695">string</span><span style="color:#444444">]</span><span style="color:#444444"> </span><span style="color:#444444">str__int</span><span style="color:#444444">;</span><br>
<br>
<span style="color:#009695">static</span><span style="color:#444444"> </span><span style="color:#009695">this</span><span style="color:#444444">(</span><span style="color:#444444">)</span><span style="color:#444444">{</span><br>
<span style="color:#444444"> </span><span style="color:#009695">for</span><span style="color:#444444"> </span><span style="color:#444444">(</span><span style="color:#009695">int</span><span style="color:#444444"> </span><span style="color:#444444">i</span><span style="color:#444444"> </span><span style="color:#444444">=</span><span style="color:#444444"> </span><span style="color:#f57d00">0</span><span style="color:#444444">;</span><span style="color:#444444"> </span><span style="color:#444444">i</span><span style="color:#444444"> </span><span style="color:#444444"><</span><span style="color:#444444"> </span><span style="color:#f57d00">100</span><span style="color:#444444">;</span><span style="color:#444444"> </span><span style="color:#444444">+</span><span style="color:#444444">+</span><span style="color:#444444">i</span><span style="color:#444444">)</span><br>
<span style="color:#444444"> </span><span style="color:#444444">{</span><br>
<span style="color:#444444"> </span><span style="color:#444444"> </span><span style="color:#009695">auto</span><span style="color:#444444"> </span><span style="color:#444444">str</span><span style="color:#444444"> </span><span style="color:#444444">=</span><span style="color:#444444"> </span><span style="color:#444444">to</span><span style="color:#444444">!</span><span style="color:#009695">string</span><span style="color:#444444">(</span><span style="color:#444444">i</span><span style="color:#444444">)</span><span style="color:#444444">;</span><br>
<span style="color:#444444"> </span><span style="color:#444444"> </span><span style="color:#444444">int__str</span><span style="color:#444444">[</span><span style="color:#444444">i</span><span style="color:#444444">]</span><span style="color:#444444"> </span><span style="color:#444444">=</span><span style="color:#444444"> </span><span style="color:#444444">str</span><span style="color:#444444">;</span><span style="color:#444444"> </span><span style="font-style:italic;color:#999988">//</span><span style="font-style:italic;color:#999988"> </span><span style="font-style:italic;color:#999988">ERROR</span><span style="font-style:italic;color:#999988">:</span><span style="font-style:italic;color:#999988"> </span><span style="font-style:italic;color:#999988">Can</span><span style="font-style:italic;color:#999988">'</span><span style="font-style:italic;color:#999988">t</span><span style="font-style:italic;color:#999988"> </span><span style="font-style:italic;color:#999988">modify</span><span style="font-style:italic;color:#999988"> </span><span style="font-style:italic;color:#999988">const</span><br>
<span style="color:#444444"> </span><span style="color:#444444"> </span><span style="color:#444444">str__int</span><span style="color:#444444">[</span><span style="color:#444444">str</span><span style="color:#444444">]</span><span style="color:#444444"> </span><span style="color:#444444">=</span><span style="color:#444444"> </span><span style="color:#444444">i</span><span style="color:#444444">;</span><span style="color:#444444"> </span><span style="font-style:italic;color:#999988">//</span><span style="font-style:italic;color:#999988"> </span><span style="font-style:italic;color:#999988">ERROR</span><span style="font-style:italic;color:#999988">:</span><span style="font-style:italic;color:#999988"> </span><span style="font-style:italic;color:#999988">Can</span><span style="font-style:italic;color:#999988">'</span><span style="font-style:italic;color:#999988">t</span><span style="font-style:italic;color:#999988"> </span><span style="font-style:italic;color:#999988">modify</span><span style="font-style:italic;color:#999988"> </span><span style="font-style:italic;color:#999988">const</span><br>
<span style="color:#444444"> </span><span style="color:#444444">}</span><br>
<span style="color:#444444">}</span><br>
</font><br><br><br><div class="gmail_quote">On Thu, May 16, 2013 at 12:57 AM, dennis luehring <span dir="ltr"><<a href="mailto:dl.soluz@gmx.net" target="_blank">dl.soluz@gmx.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
doesn't that break the constness for other threads usage?<br>
<br>
(not in your example - but in any other using threads, const globals and your proposed "initialization")<br>
<br>
Am 16.05.2013 09:53, schrieb TommiT:<div class="HOEnZb"><div class="h5"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'd like to make it easier to initialize function local<br>
immutable/const data. Here's the type of problem I'd like to<br>
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<br>
once (in the same loop or other block). If I needed to initialize<br>
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<br>
const variables at the same time.<br>
<br>
Here's my proposal: "initialization scope". You'd use it like<br>
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<br>
not a lexical scope (like static if), it merely makes all const<br>
and immutable variables created in that scope modifiable inside<br>
that scope but not after it.<br>
<br>
</blockquote>
<br>
</div></div></blockquote></div><br></div>