November 11, 2013 Re: AA literals/initialisation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | On Monday, 11 November 2013 at 11:43:01 UTC, Dmitry Olshansky wrote:
> 11-Nov-2013 15:06, Manu пишет:
>
>> but enum works fine:
>>
>> enum priorityMap = [
>>
>> "1" : "blocker",
>> "2" : "critical",
>> "3" : "critical",
>> "4" : "major",
>> "5" : "major",
>> "6" : "major",
>> "7" : "minor",
>> "8" : "minor",
>> "9" : "trivial" ];
>>
>>
>> So it does... I didn't think of an enum AA ;)
>
> ... copy pastes that literal everywhere, I'm sure you'll like it ;)
Yes, it is perfect :D
|
November 11, 2013 Re: AA literals/initialisation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | On Monday, 11 November 2013 at 11:43:01 UTC, Dmitry Olshansky
wrote:
> 11-Nov-2013 15:06, Manu пишет:
>
>> but enum works fine:
>>
>> enum priorityMap = [
>>
>> "1" : "blocker",
>> "2" : "critical",
>> "3" : "critical",
>> "4" : "major",
>> "5" : "major",
>> "6" : "major",
>> "7" : "minor",
>> "8" : "minor",
>> "9" : "trivial" ];
>>
>>
>> So it does... I didn't think of an enum AA ;)
>
> ... copy pastes that literal everywhere, I'm sure you'll like it ;)
Will it be copied even if you just use it ("1" in priorityMap),
or only if you pass it around as a parameter or assign it to
variables?
|
November 11, 2013 Re: AA literals/initialisation | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 11-Nov-2013 15:55, simendsjo пишет: > On Monday, 11 November 2013 at 11:43:01 UTC, Dmitry Olshansky > wrote: >> 11-Nov-2013 15:06, Manu пишет: >> >>> but enum works fine: >>> >>> enum priorityMap = [ >>> >>> "1" : "blocker", >>> "2" : "critical", >>> "3" : "critical", >>> "4" : "major", >>> "5" : "major", >>> "6" : "major", >>> "7" : "minor", >>> "8" : "minor", >>> "9" : "trivial" ]; >>> >>> >>> So it does... I didn't think of an enum AA ;) >> >> ... copy pastes that literal everywhere, I'm sure you'll like it ;) > > Will it be copied even if you just use it ("1" in priorityMap), > or only if you pass it around as a parameter or assign it to > variables? It will do whatever it does when written as literal by hand. And it's nothing clever still, last time I checked - allocation, everywhere. -- Dmitry Olshansky |
November 11, 2013 Re: AA literals/initialisation | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | On Monday, November 11, 2013 12:55:09 simendsjo wrote:
> On Monday, 11 November 2013 at 11:43:01 UTC, Dmitry Olshansky
>
> wrote:
> > 11-Nov-2013 15:06, Manu пишет:
> >> but enum works fine:
> >>
> >> enum priorityMap = [
> >>
> >> "1" : "blocker",
> >> "2" : "critical",
> >> "3" : "critical",
> >> "4" : "major",
> >> "5" : "major",
> >> "6" : "major",
> >> "7" : "minor",
> >> "8" : "minor",
> >> "9" : "trivial" ];
> >>
> >> So it does... I didn't think of an enum AA ;)
> >
> > ... copy pastes that literal everywhere, I'm sure you'll like it ;)
>
> Will it be copied even if you just use it ("1" in priorityMap), or only if you pass it around as a parameter or assign it to variables?
Every time you use an enum, it's replaced with its value. So, if an enum is an AA, then that literal is copy-pasted everywhere that the enum is used. So, it would almost certainly be foolish to use it anywhere other than to assign to a variable (and then all of the operations are done on the variable). Really, having an enum that's an AA is almost certainly a foolish thing to do. It's one case where the behavior of enums doesn't help and definitely hurts.
- Jonathan M Davis
|
November 11, 2013 Re: AA literals/initialisation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | enum - compile time constant immutable - run time constant thread about it was somewhere here |
November 11, 2013 Re: AA literals/initialisation | ||||
---|---|---|---|---|
| ||||
Posted in reply to OneTwo | On Monday, November 11, 2013 13:42:17 OneTwo wrote:
> enum - compile time constant
> immutable - run time constant
Not exactly. If they're global or static, then they both have to be known at compile time (though you can initialize an immutable global or static in a static constructor, which you can't do with an enum). The primary difference is that an immutable variable is actually a variable with a location in memory, where an enum is just a value that gets copy-pasted wherever it's used and is not associated with any particular location in memory. So, which you use tends to depend on whether you need it to be associated with a particular address in memory and whether you want its value to be copied everywhere.
- Jonathan M Davis
|
November 12, 2013 Re: AA literals/initialisation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Monday, 11 November 2013 at 12:14:10 UTC, Jonathan M Davis wrote:
> Every time you use an enum, it's replaced with its value. So, if an enum is an
> AA, then that literal is copy-pasted everywhere that the enum is used. So, it
> would almost certainly be foolish to use it anywhere other than to assign to a
> variable (and then all of the operations are done on the variable). Really,
> having an enum that's an AA is almost certainly a foolish thing to do. It's
> one case where the behavior of enums doesn't help and definitely hurts.
>
> - Jonathan M Davis
I have found it to be kind of usefull atleast when i'm only using the AA at compiletime. Like a simple vector swizzle like the code below. I store rgba/xyzw characters in the table with diffrent offsets into a static array.
Vector!(s.length, T) opDispatch(string s)()
if(s.length > 1)
{
Vector!(s.length, T) res;
foreach(i; staticIota!(0, s.length)) {
enum offset = swizzleTable[s[i]];
res.data[i] = data[offset];
}
return res;
}
However with your statement above i'm now a little worried does this mean that the line enum offset = swizzleTable[s[i]]; will not be able to pick the correct constant at compiletime? And will instead do some runtime AA hash lookup?
|
November 12, 2013 Re: AA literals/initialisation | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheFlyingFiddle | On Tuesday, November 12, 2013 01:07:21 TheFlyingFiddle wrote:
> On Monday, 11 November 2013 at 12:14:10 UTC, Jonathan M Davis
>
> wrote:
> > Every time you use an enum, it's replaced with its value. So,
> > if an enum is an
> > AA, then that literal is copy-pasted everywhere that the enum
> > is used. So, it
> > would almost certainly be foolish to use it anywhere other than
> > to assign to a
> > variable (and then all of the operations are done on the
> > variable). Really,
> > having an enum that's an AA is almost certainly a foolish thing
> > to do. It's
> > one case where the behavior of enums doesn't help and
> > definitely hurts.
> >
> > - Jonathan M Davis
>
> I have found it to be kind of usefull atleast when i'm only using the AA at compiletime. Like a simple vector swizzle like the code below. I store rgba/xyzw characters in the table with diffrent offsets into a static array.
>
> Vector!(s.length, T) opDispatch(string s)()
> if(s.length > 1)
> {
> Vector!(s.length, T) res;
> foreach(i; staticIota!(0, s.length)) {
> enum offset = swizzleTable[s[i]];
> res.data[i] = data[offset];
> }
> return res;
> }
>
> However with your statement above i'm now a little worried does this mean that the line enum offset = swizzleTable[s[i]]; will not be able to pick the correct constant at compiletime? And will instead do some runtime AA hash lookup?
All enum's must be known at compile time. They are never calculated at runtime. It's the _value_ of an enum that gets copy-pasted, not its text. So, if you have
enum offset = swizzleTable[s[i]];
then the result will be calculated at compile time. Whether calculating it is at all efficient is quite another matter, but the calculation will be done at compile time.
- Jonathan M Davis
|
November 12, 2013 Re: AA literals/initialisation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Murphy | On Monday, 11 November 2013 at 11:39:06 UTC, Daniel Murphy wrote:
> "Manu" <turkeyman@gmail.com> wrote in message
> news:mailman.355.1384158631.9546.digitalmars-d@puremagic.com...
>> immutable string[string] priorityMap = [
>> "1" : "blocker",
>> "2" : "critical",
>> "3" : "critical",
>> "4" : "major",
>> "5" : "major",
>> "6" : "major",
>> "7" : "minor",
>> "8" : "minor",
>> "9" : "trivial" ];
>>
>> main.d(56): Error: non-constant expression ["1":"blocker", "2":"critical",
>> "3":"critical", "4":"major", "5":"major", "6":"major", "7":"minor",
>> "8":"minor", "9":"trivial"]
>>
>> This is tedious, how long has it been now?
>> Seriously, static map's are super-important, they should be able to be made
>> immutable, and also be able to be initialised.
>>
>> Maybe this could be factored into the improvements for 2.065?
>>
>
> I think yes, it can be done for 2.065. Someone remind me if we get close
> and it isn't done yet.
IIRC the poor performance of array literals and AA literals is because they're not always literals, sometimes they are variables (!) and the compiler assumes the worst case. You are allowed to write:
void foo(int some_param)
{
immutable string[int] = [ 1: "abc", some_param: "def"];
}
I wish we could get rid of that silliness entirely.
If the members are compile-time expressions, you probably want to mark the variable as static const/static immutable.
|
November 12, 2013 Re: AA literals/initialisation | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don | "Don" <x@nospam.com> wrote in message news:bdmmimwuqsgphgprdngh@forum.dlang.org... > > IIRC the poor performance of array literals and AA literals is because they're not always literals, sometimes they are variables (!) and the compiler assumes the worst case. You are allowed to write: > > void foo(int some_param) > { > immutable string[int] = [ 1: "abc", some_param: "def"]; > } > > I wish we could get rid of that silliness entirely. > > > If the members are compile-time expressions, you probably want to mark the variable as static const/static immutable. Yeah, IIRC the missing piece is putting AAs in the data segment so they can cross from compile to run-time with out an aaliteral call. At least then we could use enum aas in runtime code without allocating. It would be great if we could make aa literals default to immutable (array literals too), and force adding .dup to get a mutable version. |
Copyright © 1999-2021 by the D Language Foundation