www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - New feature proposal: "initialization scope"

reply "TommiT" <tommitissari hotmail.com> writes:
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.
May 16 2013
next sibling parent reply dennis luehring <dl.soluz gmx.net> writes:
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.

May 16 2013
parent dennis luehring <dl.soluz gmx.net> writes:
Am 16.05.2013 10:09, schrieb TommiT:
 On Thursday, 16 May 2013 at 07:59:20 UTC, dennis luehring 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")

I'm sorry, somehow I just don't understand the question.

in short: your proposal breaks const thread access safetiness and there are 3 other solutions down here 1. static this () { } 2. 2 functions returning into consts 3. a class with init in ctor + lookup funcs in const ref so there is not need for an "initialization" feature
May 16 2013
prev sibling next sibling parent reply "deadalnix" <deadalnix gmail.com> writes:
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 ?
May 16 2013
parent dennis luehring <dl.soluz gmx.net> writes:
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
May 16 2013
prev sibling next sibling parent Timothee Cour <thelastmammoth gmail.com> writes:
--047d7b33ce8c42282804dcd1469b
Content-Type: text/plain; charset=ISO-8859-1

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 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.


--047d7b33ce8c42282804dcd1469b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable this works:<div><br></div><div> <font face=3D"Menlo"> <br> <span style=3D"color:#009695">import</span><span style=3D"color:#444444">= =A0</span><span style=3D"color:#444444">std</span><span style=3D"color:#444= 444">.</span><span style=3D"color:#444444">stdio</span><span style=3D"color= :#444444">;</span><br> <span style=3D"color:#009695">import</span><span style=3D"color:#444444">= =A0</span><span style=3D"color:#444444">std</span><span style=3D"color:#444= 444">.</span><span style=3D"color:#444444">conv</span><span style=3D"color:= #444444">;</span><br> <span style=3D"color:#009695">const</span><span style=3D"color:#444444">=A0= </span><span style=3D"color:#009695">string</span><span style=3D"color:#444= 444">[</span><span style=3D"color:#f57d00">100</span><span style=3D"color:#= 444444">]</span><span style=3D"color:#444444">=A0</span><span style=3D"colo= r:#444444">int__str</span><span style=3D"color:#444444">;</span><br> <span style=3D"color:#009695">const</span><span style=3D"color:#444444">=A0= </span><span style=3D"color:#009695">int</span><span style=3D"color:#444444= ">[</span><span style=3D"color:#009695">string</span><span style=3D"color:#= 444444">]</span><span style=3D"color:#444444">=A0</span><span style=3D"colo= r:#444444">str__int</span><span style=3D"color:#444444">;</span><br> <br> <span style=3D"color:#009695">static</span><span style=3D"color:#444444">= =A0</span><span style=3D"color:#009695">this</span><span style=3D"color:#44= 4444">(</span><span style=3D"color:#444444">)</span><span style=3D"color:#4= 44444">{</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#0096= 95">for</span><span style=3D"color:#444444">=A0</span><span style=3D"color:= #444444">(</span><span style=3D"color:#009695">int</span><span style=3D"col= or:#444444">=A0</span><span style=3D"color:#444444">i</span><span style=3D"= color:#444444">=A0</span><span style=3D"color:#444444">=3D</span><span styl= e=3D"color:#444444">=A0</span><span style=3D"color:#f57d00">0</span><span s= tyle=3D"color:#444444">;</span><span style=3D"color:#444444">=A0</span><spa= n style=3D"color:#444444">i</span><span style=3D"color:#444444">=A0</span><= span style=3D"color:#444444">&lt;</span><span style=3D"color:#444444">=A0</= span><span style=3D"color:#f57d00">100</span><span style=3D"color:#444444">= ;</span><span style=3D"color:#444444">=A0</span><span style=3D"color:#44444= 4">+</span><span style=3D"color:#444444">+</span><span style=3D"color:#4444= 44">i</span><span style=3D"color:#444444">)</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">{</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">=A0=A0=A0=A0</span><span style=3D"color:#009695">auto</span><span style= =3D"color:#444444">=A0</span><span style=3D"color:#444444">str</span><span = style=3D"color:#444444">=A0</span><span style=3D"color:#444444">=3D</span><= span style=3D"color:#444444">=A0</span><span style=3D"color:#444444">to</sp= an><span style=3D"color:#444444">!</span><span style=3D"color:#009695">stri= ng</span><span style=3D"color:#444444">(</span><span style=3D"color:#444444= ">i</span><span style=3D"color:#444444">)</span><span style=3D"color:#44444= 4">;</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">=A0=A0=A0=A0</span><span style=3D"color:#444444">int__str</span><span s= tyle=3D"color:#444444">[</span><span style=3D"color:#444444">i</span><span = style=3D"color:#444444">]</span><span style=3D"color:#444444">=A0</span><sp= an style=3D"color:#444444">=3D</span><span style=3D"color:#444444">=A0</spa= n><span style=3D"color:#444444">str</span><span style=3D"color:#444444">;</= span><span style=3D"color:#444444">=A0</span><span style=3D"font-style:ital= ic;color:#999988">//</span><span style=3D"font-style:italic;color:#999988">= =A0</span><span style=3D"font-style:italic;color:#999988">ERROR</span><span= style=3D"font-style:italic;color:#999988">:</span><span style=3D"font-styl= e:italic;color:#999988">=A0</span><span style=3D"font-style:italic;color:#9= 99988">Can</span><span style=3D"font-style:italic;color:#999988">&#39;</spa= n><span style=3D"font-style:italic;color:#999988">t</span><span style=3D"fo= nt-style:italic;color:#999988">=A0</span><span style=3D"font-style:italic;c= olor:#999988">modify</span><span style=3D"font-style:italic;color:#999988">= =A0</span><span style=3D"font-style:italic;color:#999988">const</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">=A0=A0=A0=A0</span><span style=3D"color:#444444">str__int</span><span s= tyle=3D"color:#444444">[</span><span style=3D"color:#444444">str</span><spa= n style=3D"color:#444444">]</span><span style=3D"color:#444444">=A0</span><= span style=3D"color:#444444">=3D</span><span style=3D"color:#444444">=A0</s= pan><span style=3D"color:#444444">i</span><span style=3D"color:#444444">;</= span><span style=3D"color:#444444">=A0</span><span style=3D"font-style:ital= ic;color:#999988">//</span><span style=3D"font-style:italic;color:#999988">= =A0</span><span style=3D"font-style:italic;color:#999988">ERROR</span><span= style=3D"font-style:italic;color:#999988">:</span><span style=3D"font-styl= e:italic;color:#999988">=A0</span><span style=3D"font-style:italic;color:#9= 99988">Can</span><span style=3D"font-style:italic;color:#999988">&#39;</spa= n><span style=3D"font-style:italic;color:#999988">t</span><span style=3D"fo= nt-style:italic;color:#999988">=A0</span><span style=3D"font-style:italic;c= olor:#999988">modify</span><span style=3D"font-style:italic;color:#999988">= =A0</span><span style=3D"font-style:italic;color:#999988">const</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">}</span><br> <span style=3D"color:#444444">}</span><br> </font><br><br><br><div class=3D"gmail_quote">On Thu, May 16, 2013 at 12:57= AM, dennis luehring <span dir=3D"ltr">&lt;<a href=3D"mailto:dl.soluz gmx.n= et" target=3D"_blank">dl.soluz gmx.net</a>&gt;</span> wrote:<br><blockquote= class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc soli= d;padding-left:1ex"> doesn&#39;t that break the constness for other threads usage?<br> <br> (not in your example - but in any other using threads, const globals and yo= ur proposed &quot;initialization&quot;)<br> <br> Am 16.05.2013 09:53, schrieb TommiT:<div class=3D"HOEnZb"><div class=3D"h5"=
<br>

x #ccc solid;padding-left:1ex"> I&#39;d like to make it easier to initialize function local<br> immutable/const data. Here&#39;s the type of problem I&#39;d like to<br> alleviate:<br> <br> const string[100] int__str;<br> const int[string] str__int;<br> <br> for (int i =3D 0; i &lt; 100; ++i)<br> {<br> =A0 =A0 =A0 auto str =3D to!string(i);<br> =A0 =A0 =A0 int__str[i] =3D str; // ERROR: Can&#39;t modify const<br> =A0 =A0 =A0 str__int[str] =3D i; // ERROR: Can&#39;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 =3D {<br> =A0 =A0 =A0 string[100] tmp;<br> =A0 =A0 =A0 // ... init tmp ...<br> =A0 =A0 =A0 return tmp;<br> }();<br> <br> ...But I can&#39;t see any easy solution for initializing two or more<br> const variables at the same time.<br> <br> Here&#39;s my proposal: &quot;initialization scope&quot;. You&#39;d use it = like<br> this:<br> <br> initialization {<br> =A0 =A0 =A0 const string[100] int__str;<br> =A0 =A0 =A0 const int[string] str__int;<br> <br> =A0 =A0 =A0 for (int i =3D 0; i &lt; 100; ++i)<br> =A0 =A0 =A0 {<br> =A0 =A0 =A0 =A0 =A0 auto str =3D to!string(i);<br> =A0 =A0 =A0 =A0 =A0 int__str[i] =3D str; // OK<br> =A0 =A0 =A0 =A0 =A0 str__int[str] =3D i; // OK<br> =A0 =A0 =A0 }<br> }<br> <br> string s =3D int__str[42]; // OK<br> int__str[42] =3D &quot;43&quot; // ERROR: Can&#39;t modify const<br> <br> As you can see, &#39;initialization scope&#39; 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> --047d7b33ce8c42282804dcd1469b--
May 16 2013
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2013-05-16 09:53, 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).

Use a module constructor: static this () { } http://dlang.org/module.html#staticorder -- /Jacob Carlborg
May 16 2013
parent dennis luehring <dl.soluz gmx.net> writes:
Am 16.05.2013 10:14, schrieb TommiT:
 On Thursday, 16 May 2013 at 08:02:05 UTC, Jacob Carlborg wrote:
 Use a module constructor:

 static this () { }

 http://dlang.org/module.html#staticorder

On Thursday, 16 May 2013 at 08:01:44 UTC, Timothee Cour wrote:
 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
     }
 }

But I'm talking about function local data. The initialization of that data might depend on the arguments passed to the function.

so youre example code isn't showing what you try to reach - makes it hard to understand/help - its even unclear if your string[100], int[string] is just an example or a real problem, or if your < 100 is variant or const - come up with an clear example the normal way of doing stuff like this in C++,Java and D would be and helper class with const members and const methods for access
May 16 2013
prev sibling next sibling parent "TommiT" <tommitissari hotmail.com> writes:
On Thursday, 16 May 2013 at 07:53:08 UTC, TommiT wrote:
 string s = int__str[42]; // OK
 int__str[42] = "43" // ERROR: Can't modify const

I should have said: int n = str__int["42"]; str__int["42"] = 43; // ERROR: Can't modify str__int
May 16 2013
prev sibling next sibling parent "TommiT" <tommitissari hotmail.com> writes:
On Thursday, 16 May 2013 at 07:59:20 UTC, dennis luehring 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")

I'm sorry, somehow I just don't understand the question.
May 16 2013
prev sibling next sibling parent "TommiT" <tommitissari hotmail.com> writes:
On Thursday, 16 May 2013 at 08:02:05 UTC, Jacob Carlborg wrote:
 Use a module constructor:

 static this () { }

 http://dlang.org/module.html#staticorder

On Thursday, 16 May 2013 at 08:01:44 UTC, Timothee Cour wrote:
 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
     }
 }

But I'm talking about function local data. The initialization of that data might depend on the arguments passed to the function.
May 16 2013
prev sibling next sibling parent "TommiT" <tommitissari hotmail.com> writes:
On Thursday, 16 May 2013 at 08:01:33 UTC, deadalnix wrote:
 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 ?

I could also argue that it's just not very elegant solution. You're left with this extra, useless dummy variable (the initial, mutable one) that you're not supposed to use. Also, I'd like to initialize immutable variables plus there's the possible performance issue (don't have time to do a test now).
May 16 2013
prev sibling next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Thursday, May 16, 2013 10:20:16 TommiT wrote:
 On Thursday, 16 May 2013 at 08:01:33 UTC, deadalnix wrote:
 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 ?

I could also argue that it's just not very elegant solution. You're left with this extra, useless dummy variable (the initial, mutable one) that you're not supposed to use. Also, I'd like to initialize immutable variables plus there's the possible performance issue (don't have time to do a test now).

Well, that's the standard way to do it. If you can't initialize a const or immutable variable in one go, you create a mutable one and then cast it (or use assumeUnique) on it and assign it to the const/immutable variable that you wanted to initialize. For module-level and static variables, you'd generally do it in a static constructor, though that isn't an option for local variables. In their case though, if you don't want two variables floating around, you can always use a nested function (or some other function) to generate the mutable variable and then return it as const or immutable. Normally, there's no performance hit to doing any of this, but it is true that that's a potential issue in your example, because it's a static variable. I question that that merits adding a new feature to the language though. The trick of creating it as mutable and then casting it to immutable works quite well overall and really hasn't been causing problems. - Jonathan M Davis
May 16 2013
prev sibling next sibling parent "Dicebot" <m.strashun gmail.com> writes:
Against.
May 16 2013
prev sibling next sibling parent Timothee Cour <thelastmammoth gmail.com> writes:
--089e0118289ec6c7d104dcd1fdb7
Content-Type: text/plain; charset=ISO-8859-1

what's more fundamentally missing is ability to use ref on variables. DIP39
should be able to be adapted to that case.

 void main(){
    int[1000] x1_;
    int[1000] x2_;
    // do some initialization on x1_,x2_
    const(typeof(x)) x1=x1_;  //wishful thinking
    const(typeof(x)) x2=x2_;  //ditto
    assert(&x1 is &x1_); // no copy
}

--089e0118289ec6c7d104dcd1fdb7
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div><font face=3D"Menlo"><span style=3D"color:#009695">what&#39;s more fun=
damentally missing is ability to use ref on variables. DIP39 should be able=
 to be adapted to that case.</span></font></div><div><font face=3D"Menlo"><=
span style=3D"color:#009695"><br>
</span></font></div>



<font face=3D"Menlo">
<span style=3D"color:#009695">void</span><span style=3D"color:#444444">=A0<=
/span><span style=3D"color:#444444">main</span><span style=3D"color:#444444=
">(</span><span style=3D"color:#444444">)</span><span style=3D"color:#44444=
4">{</span><br>
</font><div><font face=3D"Menlo"><span style=3D"color:rgb(68,68,68)">=A0 =
=A0=A0</span><span style=3D"color:rgb(0,150,149)">int</span><span style=3D"=
color:rgb(68,68,68)">[</span><span style=3D"color:rgb(245,125,0)">1000</spa=
n><span style=3D"color:rgb(68,68,68)">]</span><span style=3D"color:rgb(68,6=
8,68)">=A0</span><span style=3D"color:rgb(68,68,68)">x1_</span><span style=
=3D"color:rgb(68,68,68)">;</span></font></div>
<font face=3D"Menlo"><span style=3D"color:rgb(68,68,68)">=A0 =A0=A0</span><=
span style=3D"color:rgb(0,150,149)">int</span><span style=3D"color:rgb(68,6=
8,68)">[</span><span style=3D"color:rgb(245,125,0)">1000</span><span style=
=3D"color:rgb(68,68,68)">]</span><span style=3D"color:rgb(68,68,68)">=A0</s=
pan><span style=3D"color:rgb(68,68,68)">x2_</span><span style=3D"color:rgb(=
68,68,68)">;</span></font><div>
<font face=3D"Menlo"><font color=3D"#444444">=A0 =A0 // do some initializat=
ion on=A0</font></font><span style=3D"color:rgb(68,68,68);font-family:Menlo=
">x1_,x2_</span></div><div><font face=3D"Menlo"><span style=3D"color:rgb(0,=
150,149)">=A0 =A0 const</span><span style=3D"color:rgb(68,68,68)">(</span><=
span style=3D"color:rgb(0,150,149)">typeof</span><span style=3D"color:rgb(6=
8,68,68)">(</span><span style=3D"color:rgb(68,68,68)">x</span><span style=
=3D"color:rgb(68,68,68)">)</span><span style=3D"color:rgb(68,68,68)">)</spa=
n><span style=3D"color:rgb(68,68,68)">=A0</span><span style=3D"color:rgb(68=
,68,68)">x1</span><span style=3D"color:rgb(68,68,68)">=3D</span><span style=
=3D"color:rgb(68,68,68)">x1_</span><span style=3D"color:rgb(68,68,68)">;</s=
pan></font>=A0=A0<span style=3D"color:rgb(68,68,68);font-family:Menlo">//wi=
shful thinking=A0</span></div>
<div><font face=3D"Menlo"><span style=3D"color:rgb(0,150,149)">=A0 =A0 cons=
t</span><span style=3D"color:rgb(68,68,68)">(</span><span style=3D"color:rg=
b(0,150,149)">typeof</span><span style=3D"color:rgb(68,68,68)">(</span><spa=
n style=3D"color:rgb(68,68,68)">x</span><span style=3D"color:rgb(68,68,68)"=
)</span><span style=3D"color:rgb(68,68,68)">)</span><span style=3D"color:r=

style=3D"color:rgb(68,68,68)">=3D</span><span style=3D"color:rgb(68,68,68)"=
x2_</span><span style=3D"color:rgb(68,68,68)">;</span></font>=A0=A0<span s=

<div><span style=3D"color:rgb(68,68,68);font-family:Menlo">=A0 =A0 assert(&= amp;x1 is &amp;x1_); // no copy</span></div><div><div><font face=3D"Menlo">= <span style=3D"color:#444444">}</span></font></div></div> --089e0118289ec6c7d104dcd1fdb7--
May 16 2013
prev sibling next sibling parent Kenji Hara <k.hara.pg gmail.com> writes:
--001a11c37f5ac0f14404dcd2066d
Content-Type: text/plain; charset=UTF-8

2013/5/16 TommiT <tommitissari hotmail.com>

 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.

Pure delegate and implicit conversion for unique expression should work. import std.conv; void main() { immutable string[100] int__str = () pure { string[100] tmp; debug printf("&tmp[0] = %p\n", &tmp[0]); // [1] for (int i = 0; i < 100; ++i) { auto str = to!string(i); // BUG: pure function '__lambda1' cannot call impure function 'to' tmp[i] = str; } return tmp; // BUG: NRVO doesn't work }(); debug printf("&int__str[0] = %p\n", &int__str[0]); // [2] } Compiler bug: Currently [1] and [2] does not print same address. Phobos bug: Currently std.conv.to is not pure. Kenji Hara --001a11c37f5ac0f14404dcd2066d Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">2013= /5/16 TommiT <span dir=3D"ltr">&lt;<a href=3D"mailto:tommitissari hotmail.c= om" target=3D"_blank">tommitissari hotmail.com</a>&gt;</span><br><blockquot= e class=3D"gmail_quote" style=3D"margin-top:0px;margin-right:0px;margin-bot= tom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204,2= 04,204);border-left-style:solid;padding-left:1ex"> I&#39;d like to make it easier to initialize function local immutable/const= data. Here&#39;s the type of problem I&#39;d like to alleviate:<br> <br> const string[100] int__str;<br> const int[string] str__int;<br> <br> for (int i =3D 0; i &lt; 100; ++i)<br> {<br> =C2=A0 =C2=A0 auto str =3D to!string(i);<br> =C2=A0 =C2=A0 int__str[i] =3D str; // ERROR: Can&#39;t modify const<br> =C2=A0 =C2=A0 str__int[str] =3D i; // ERROR: Can&#39;t modify const<br> }<br> <br> In short, I want to initialize two different const variables at once (in th= e same loop or other block). If I needed to initialize only one const varia= ble, I could use a lambda:<br> <br> const string[100] int__str =3D {<br> =C2=A0 =C2=A0 string[100] tmp;<br> =C2=A0 =C2=A0 // ... init tmp ...<br> =C2=A0 =C2=A0 return tmp;<br> }();<br> <br> ...But I can&#39;t see any easy solution for initializing two or more const= variables at the same time.<br> <br> Here&#39;s my proposal: &quot;initialization scope&quot;. You&#39;d use it = like this:<br> <br> initialization {<br> =C2=A0 =C2=A0 const string[100] int__str;<br> =C2=A0 =C2=A0 const int[string] str__int;<br> <br> =C2=A0 =C2=A0 for (int i =3D 0; i &lt; 100; ++i)<br> =C2=A0 =C2=A0 {<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto str =3D to!string(i);<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 int__str[i] =3D str; // OK<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 str__int[str] =3D i; // OK<br> =C2=A0 =C2=A0 }<br> }<br> <br> string s =3D int__str[42]; // OK<br> int__str[42] =3D &quot;43&quot; // ERROR: Can&#39;t modify const<br> <br> As you can see, &#39;initialization scope&#39; 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=3D"gmail_extra">Pure delegate and i= mplicit conversion for unique expression should work.</div><div class=3D"gm= ail_extra"><br></div><div class=3D"gmail_extra">import std.conv;</div><div = class=3D"gmail_extra"> <div class=3D"gmail_extra">void main()</div><div class=3D"gmail_extra">{</d= iv><div class=3D"gmail_extra">=C2=A0 =C2=A0 immutable string[100] int__str = =3D () pure {</div><div class=3D"gmail_extra">=C2=A0 =C2=A0 =C2=A0 =C2=A0 s= tring[100] tmp;</div><div class=3D"gmail_extra"> =C2=A0 =C2=A0 =C2=A0 =C2=A0 debug printf(&quot;&amp;tmp[0] =3D %p\n&quot;, = &amp;tmp[0]); =C2=A0 =C2=A0 =C2=A0 =C2=A0// [1]</div><div class=3D"gmail_ex= tra">=C2=A0 =C2=A0 =C2=A0 =C2=A0 for (int i =3D 0; i &lt; 100; ++i)</div><d= iv class=3D"gmail_extra">=C2=A0 =C2=A0 =C2=A0 =C2=A0 {</div><div class=3D"g= mail_extra"> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 auto str =3D to!string(i);</div><= div class=3D"gmail_extra">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // BUG:= pure function &#39;__lambda1&#39; cannot call impure function &#39;to&#39;= </div><div class=3D"gmail_extra">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = tmp[i] =3D str;</div> <div class=3D"gmail_extra">=C2=A0 =C2=A0 =C2=A0 =C2=A0 }</div><div class=3D= "gmail_extra">=C2=A0 =C2=A0 =C2=A0 =C2=A0 return tmp; // BUG: NRVO doesn&#3= 9;t work</div><div class=3D"gmail_extra">=C2=A0 =C2=A0 }();</div><div class= =3D"gmail_extra">=C2=A0 =C2=A0 debug printf(&quot;&amp;int__str[0] =3D %p\n= &quot;, &amp;int__str[0]); =C2=A0// [2]</div> <div class=3D"gmail_extra">}</div><div class=3D"gmail_extra"><br></div><div= class=3D"gmail_extra">Compiler bug: Currently [1] and [2] does not print s= ame address.</div><div class=3D"gmail_extra">Phobos bug: Currently <a href= =3D"http://std.conv.to">std.conv.to</a> is not pure.</div> <div><br></div><div>Kenji Hara</div></div></div> --001a11c37f5ac0f14404dcd2066d--
May 16 2013
prev sibling next sibling parent "TommiT" <tommitissari hotmail.com> writes:
On Thursday, 16 May 2013 at 08:31:27 UTC, Jonathan M Davis wrote:
 Normally, there's no performance hit to doing any of this, but 
 it is true that
 that's a potential issue in your example, because it's a static 
 variable. [..]

I assume you mean the variable is statically allocated. Which, I assume, means that there's an actual memory copy involved when such variable is cast to immutable. Here's some more of the logic behind my suggestion: Any hoops, that the programmer has to go through in order to make his function-local variable a const/immutable, discourages him from making the variable const/immutable. To me, it seems that the path of least resistance for the programmer is simply not make it const/immutable. By discouraging const-correctness we encourage writing bugs.
May 16 2013
prev sibling next sibling parent Timothee Cour <thelastmammoth gmail.com> writes:
--047d7b3a831ceffa5804dcd2c4ef
Content-Type: text/plain; charset=ISO-8859-1

simplified case:

doesn't work with NRVO:
 void main(){
    immutable int[10] x = () pure {
        int[10] x_;
        debug printf("&x_[0] = %p\n", &x_[0]);
        return x_;
    }();
    debug printf("&x[0] = %p\n", &x[0]);  // prints different address
}


doesn't work with RVO:
 struct A{
    int[10] x_;
    this(int _ignore)pure{
        foo();
    }
    void foo()const pure{
        debug printf("&x_[0] = %p\n", &x_[0]);
    }
}
void main(){
    immutable A a = () pure {
        return A(0);
    }();
    a.foo();
}


is RVO even happening?

On Thu, May 16, 2013 at 1:55 AM, Kenji Hara <k.hara.pg gmail.com> wrote:

 2013/5/16 TommiT <tommitissari hotmail.com>

 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.

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

--047d7b3a831ceffa5804dcd2c4ef Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable simplified case:<div><br></div><div>doesn&#39;t work with NRVO:<br><div> <font face=3D"Menlo"> <span style=3D"color:#009695">void</span><span style=3D"color:#444444">=A0<= /span><span style=3D"color:#444444">main</span><span style=3D"color:#444444= ">(</span><span style=3D"color:#444444">)</span><span style=3D"color:#44444= 4">{</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#0096= 95">immutable</span><span style=3D"color:#444444">=A0</span><span style=3D"= color:#009695">int</span><span style=3D"color:#444444">[</span><span style= =3D"color:#f57d00">10</span><span style=3D"color:#444444">]</span><span sty= le=3D"color:#444444">=A0</span><span style=3D"color:#444444">x</span><span = style=3D"color:#444444">=A0</span><span style=3D"color:#444444">=3D</span><= span style=3D"color:#444444">=A0</span><span style=3D"color:#444444">(</spa= n><span style=3D"color:#444444">)</span><span style=3D"color:#444444">=A0</= span><span style=3D"color:#009695">pure</span><span style=3D"color:#444444"=
=A0</span><span style=3D"color:#444444">{</span><br>

<span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">=A0=A0=A0=A0</span><span style=3D"color:#009695">int</span><span style= =3D"color:#444444">[</span><span style=3D"color:#f57d00">10</span><span sty= le=3D"color:#444444">]</span><span style=3D"color:#444444">=A0</span><span = style=3D"color:#444444">x_</span><span style=3D"color:#444444">;</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">=A0=A0=A0=A0</span><span style=3D"color:#009695">debug</span><span styl= e=3D"color:#444444">=A0</span><span style=3D"color:#444444">printf</span><s= pan style=3D"color:#444444">(</span><span style=3D"color:#f57d00">&quot;</s= pan><span style=3D"color:#f57d00">&amp;x_[0]=A0=3D=A0%p</span><span style= =3D"color:#a53e00">\n</span><span style=3D"color:#f57d00">&quot;</span><spa= n style=3D"color:#444444">,</span><span style=3D"color:#444444">=A0</span><= span style=3D"color:#444444">&amp;</span><span style=3D"color:#444444">x_</= span><span style=3D"color:#444444">[</span><span style=3D"color:#f57d00">0<= /span><span style=3D"color:#444444">]</span><span style=3D"color:#444444">)= </span><span style=3D"color:#444444">;</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">=A0=A0=A0=A0</span><span style=3D"color:#009695">return</span><span sty= le=3D"color:#444444">=A0</span><span style=3D"color:#444444">x_</span><span= style=3D"color:#444444">;</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">}</span><span style=3D"color:#444444">(</span><span style=3D"color:#444= 444">)</span><span style=3D"color:#444444">;</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#0096= 95">debug</span><span style=3D"color:#444444">=A0</span><span style=3D"colo= r:#444444">printf</span><span style=3D"color:#444444">(</span><span style= =3D"color:#f57d00">&quot;</span><span style=3D"color:#f57d00">&amp;x[0]=A0= =3D=A0%p</span><span style=3D"color:#a53e00">\n</span><span style=3D"color:= #f57d00">&quot;</span><span style=3D"color:#444444">,</span><span style=3D"= color:#444444">=A0</span><span style=3D"color:#444444">&amp;</span><span st= yle=3D"color:#444444">x</span><span style=3D"color:#444444">[</span><span s= tyle=3D"color:#f57d00">0</span><span style=3D"color:#444444">]</span><span = style=3D"color:#444444">)</span><span style=3D"color:#444444">;</span><span= style=3D"color:#444444">=A0</span><span style=3D"color:#444444">=A0// prin= ts different address</span><br> <span style=3D"color:#444444">}</span></font>=A0</div><div><br></div><div><= br></div><div>doesn&#39;t work with RVO:</div><div> <font face=3D"Menlo"> <span style=3D"color:#009695">struct</span><span style=3D"color:#444444">= =A0</span><span style=3D"color:#444444">A</span><span style=3D"color:#44444= 4">{</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#0096= 95">int</span><span style=3D"color:#444444">[</span><span style=3D"color:#f= 57d00">10</span><span style=3D"color:#444444">]</span><span style=3D"color:= #444444">=A0</span><span style=3D"color:#444444">x_</span><span style=3D"co= lor:#444444">;</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#0096= 95">this</span><span style=3D"color:#444444">(</span><span style=3D"color:#= 009695">int</span><span style=3D"color:#444444">=A0</span><span style=3D"co= lor:#444444">_ignore</span><span style=3D"color:#444444">)</span><span styl= e=3D"color:#009695">pure</span><span style=3D"color:#444444">{</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">=A0=A0=A0=A0</span><span style=3D"color:#444444">foo</span><span style= =3D"color:#444444">(</span><span style=3D"color:#444444">)</span><span styl= e=3D"color:#444444">;</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">}</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#0096= 95">void</span><span style=3D"color:#444444">=A0</span><span style=3D"color= :#444444">foo</span><span style=3D"color:#444444">(</span><span style=3D"co= lor:#444444">)</span><span style=3D"color:#009695">const</span><span style= =3D"color:#444444">=A0</span><span style=3D"color:#009695">pure</span><span= style=3D"color:#444444">{</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">=A0=A0=A0=A0</span><span style=3D"color:#009695">debug</span><span styl= e=3D"color:#444444">=A0</span><span style=3D"color:#444444">printf</span><s= pan style=3D"color:#444444">(</span><span style=3D"color:#f57d00">&quot;</s= pan><span style=3D"color:#f57d00">&amp;x_[0]=A0=3D=A0%p</span><span style= =3D"color:#a53e00">\n</span><span style=3D"color:#f57d00">&quot;</span><spa= n style=3D"color:#444444">,</span><span style=3D"color:#444444">=A0</span><= span style=3D"color:#444444">&amp;</span><span style=3D"color:#444444">x_</= span><span style=3D"color:#444444">[</span><span style=3D"color:#f57d00">0<= /span><span style=3D"color:#444444">]</span><span style=3D"color:#444444">)= </span><span style=3D"color:#444444">;</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">}</span><br> <span style=3D"color:#444444">}</span><br> <span style=3D"color:#009695">void</span><span style=3D"color:#444444">=A0<= /span><span style=3D"color:#444444">main</span><span style=3D"color:#444444= ">(</span><span style=3D"color:#444444">)</span><span style=3D"color:#44444= 4">{</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#0096= 95">immutable</span><span style=3D"color:#444444">=A0</span><span style=3D"= color:#444444">A</span><span style=3D"color:#444444">=A0</span><span style= =3D"color:#444444">a</span><span style=3D"color:#444444">=A0</span><span st= yle=3D"color:#444444">=3D</span><span style=3D"color:#444444">=A0</span><sp= an style=3D"color:#444444">(</span><span style=3D"color:#444444">)</span><s= pan style=3D"color:#444444">=A0</span><span style=3D"color:#009695">pure</s= pan><span style=3D"color:#444444">=A0</span><span style=3D"color:#444444">{= </span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">=A0=A0=A0=A0</span><span style=3D"color:#009695">return</span><span sty= le=3D"color:#444444">=A0</span><span style=3D"color:#444444">A</span><span = style=3D"color:#444444">(</span><span style=3D"color:#f57d00">0</span><span= style=3D"color:#444444">)</span><span style=3D"color:#444444">;</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">}</span><span style=3D"color:#444444">(</span><span style=3D"color:#444= 444">)</span><span style=3D"color:#444444">;</span><br> <span style=3D"color:#444444">=A0=A0=A0=A0</span><span style=3D"color:#4444= 44">a</span><span style=3D"color:#444444">.</span><span style=3D"color:#444= 444">foo</span><span style=3D"color:#444444">(</span><span style=3D"color:#= 444444">)</span><span style=3D"color:#444444">;</span><br> <span style=3D"color:#444444">}</span><br> </font></div><div>=A0</div><div><br></div><div>is RVO even happening?</div>= <div><br><div class=3D"gmail_quote">On Thu, May 16, 2013 at 1:55 AM, Kenji = Hara <span dir=3D"ltr">&lt;<a href=3D"mailto:k.hara.pg gmail.com" target=3D= "_blank">k.hara.pg gmail.com</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div><div class=3D"h5"><div= class=3D"gmail_extra"><div class=3D"gmail_quote">2013/5/16 TommiT <span di= r=3D"ltr">&lt;<a href=3D"mailto:tommitissari hotmail.com" target=3D"_blank"=
tommitissari hotmail.com</a>&gt;</span><br>

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&#39;d like to make it easier to initialize function local immutable/const= data. Here&#39;s the type of problem I&#39;d like to alleviate:<br> <br> const string[100] int__str;<br> const int[string] str__int;<br> <br> for (int i =3D 0; i &lt; 100; ++i)<br> {<br> =A0 =A0 auto str =3D to!string(i);<br> =A0 =A0 int__str[i] =3D str; // ERROR: Can&#39;t modify const<br> =A0 =A0 str__int[str] =3D i; // ERROR: Can&#39;t modify const<br> }<br> <br> In short, I want to initialize two different const variables at once (in th= e same loop or other block). If I needed to initialize only one const varia= ble, I could use a lambda:<br> <br> const string[100] int__str =3D {<br> =A0 =A0 string[100] tmp;<br> =A0 =A0 // ... init tmp ...<br> =A0 =A0 return tmp;<br> }();<br> <br> ...But I can&#39;t see any easy solution for initializing two or more const= variables at the same time.<br> <br> Here&#39;s my proposal: &quot;initialization scope&quot;. You&#39;d use it = like this:<br> <br> initialization {<br> =A0 =A0 const string[100] int__str;<br> =A0 =A0 const int[string] str__int;<br> <br> =A0 =A0 for (int i =3D 0; i &lt; 100; ++i)<br> =A0 =A0 {<br> =A0 =A0 =A0 =A0 auto str =3D to!string(i);<br> =A0 =A0 =A0 =A0 int__str[i] =3D str; // OK<br> =A0 =A0 =A0 =A0 str__int[str] =3D i; // OK<br> =A0 =A0 }<br> }<br> <br> string s =3D int__str[42]; // OK<br> int__str[42] =3D &quot;43&quot; // ERROR: Can&#39;t modify const<br> <br> As you can see, &#39;initialization scope&#39; 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></div><div class=3D"gmail_extra">Pure de= legate and implicit conversion for unique expression should work.</div><div= class=3D"gmail_extra"><br></div><div class=3D"gmail_extra">import std.conv= ;</div> <div class=3D"gmail_extra"> <div class=3D"gmail_extra">void main()</div><div class=3D"gmail_extra">{</d= iv><div class=3D"gmail_extra">=A0 =A0 immutable string[100] int__str =3D ()= pure {</div><div class=3D"gmail_extra">=A0 =A0 =A0 =A0 string[100] tmp;</d= iv><div class=3D"gmail_extra"> =A0 =A0 =A0 =A0 debug printf(&quot;&amp;tmp[0] =3D %p\n&quot;, &amp;tmp[0])= ; =A0 =A0 =A0 =A0// [1]</div><div class=3D"im"><div class=3D"gmail_extra">= =A0 =A0 =A0 =A0 for (int i =3D 0; i &lt; 100; ++i)</div><div class=3D"gmail= _extra">=A0 =A0 =A0 =A0 {</div><div class=3D"gmail_extra"> =A0 =A0 =A0 =A0 =A0 =A0 auto str =3D to!string(i);</div></div><div class=3D= "gmail_extra">=A0 =A0 =A0 =A0 =A0 =A0 // BUG: pure function &#39;__lambda1&= #39; cannot call impure function &#39;to&#39;</div><div class=3D"gmail_extr= a">=A0 =A0 =A0 =A0 =A0 =A0 tmp[i] =3D str;</div> <div class=3D"gmail_extra">=A0 =A0 =A0 =A0 }</div><div class=3D"gmail_extra= ">=A0 =A0 =A0 =A0 return tmp; // BUG: NRVO doesn&#39;t work</div><div class= =3D"gmail_extra">=A0 =A0 }();</div><div class=3D"gmail_extra">=A0 =A0 debug= printf(&quot;&amp;int__str[0] =3D %p\n&quot;, &amp;int__str[0]); =A0// [2]= </div> <div class=3D"gmail_extra">}</div><div class=3D"gmail_extra"><br></div><div= class=3D"gmail_extra">Compiler bug: Currently [1] and [2] does not print s= ame address.</div><div class=3D"gmail_extra">Phobos bug: Currently <a href= =3D"http://std.conv.to" target=3D"_blank">std.conv.to</a> is not pure.</div=

<div><br></div><div>Kenji Hara</div></font></span></div></div> </blockquote></div><br></div></div> --047d7b3a831ceffa5804dcd2c4ef--
May 16 2013
prev sibling next sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Thursday, 16 May 2013 at 08:20:17 UTC, TommiT wrote:
 Also, I'd like to initialize immutable variables plus there's 
 the possible performance issue (don't have time to do a test 
 now).

I don't think you should come up with proposal if you don't want to explore the implications.
May 16 2013
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
May 16 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
May 16 2013
prev sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, May 16, 2013 11:32:39 TommiT wrote:
 On Thursday, 16 May 2013 at 08:31:27 UTC, Jonathan M Davis wrote:
 Normally, there's no performance hit to doing any of this, but
 it is true that
 that's a potential issue in your example, because it's a static
 variable. [..]

I assume you mean the variable is statically allocated. Which, I assume, means that there's an actual memory copy involved when such variable is cast to immutable.

I mistyped. I meant static array. The variable isn't static. But yes, the problem is that the array is copied when the assignment is made.
 Here's some more of the logic behind my suggestion:
 
 Any hoops, that the programmer has to go through in order to make
 his function-local variable a const/immutable, discourages him
 from making the variable const/immutable. To me, it seems that
 the path of least resistance for the programmer is simply not
 make it const/immutable. By discouraging const-correctness we
 encourage writing bugs.

Except that what you're suggesting is effectively saying that you can write to a const variable, which violates the type system. It must be fully initialized in one go if it's const or immutable. Creating it as mutable and casting avoids this problem entirely and is very easy to do. I really don't see it as much of a problem. And by simply using a nested function, you not only are able to just initialize the variable in one go, but the code is cleaner because all of the mutation is encapsulated. - Jonathan M Davis
May 16 2013