Thread overview | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 16, 2013 New feature proposal: "initialization scope" | ||||
---|---|---|---|---|
| ||||
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 Re: New feature proposal: "initialization scope" | ||||
---|---|---|---|---|
| ||||
Posted in reply to TommiT | 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 Re: New feature proposal: "initialization scope" | ||||
---|---|---|---|---|
| ||||
Posted in reply to TommiT | 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 Re: New feature proposal: "initialization scope" | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring Attachments:
| 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.
>>
>>
>
|
May 16, 2013 Re: New feature proposal: "initialization scope" | ||||
---|---|---|---|---|
| ||||
Posted in reply to TommiT | 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 Re: New feature proposal: "initialization scope" | ||||
---|---|---|---|---|
| ||||
Posted in reply to TommiT | 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 Re: New feature proposal: "initialization scope" | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | 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 Re: New feature proposal: "initialization scope" | ||||
---|---|---|---|---|
| ||||
Posted in reply to dennis luehring | 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 Re: New feature proposal: "initialization scope" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | 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 Re: New feature proposal: "initialization scope" | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | 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).
|
Copyright © 1999-2021 by the D Language Foundation