www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Global immutable AA literals

reply bearophile <bearophileHUGS lycos.com> writes:
Is it possible to modify DMD to allow a program like:


immutable int[int] aa = [1:15, 2: 7];
void main() {}


With the latest DMD it gives:
test.d(1): Error: non-constant expression [1:15,2:7]

Global immutable associative arrays are handy in script-like programs, I use
them now and then in Python (despite they are not immutable).
This is a workaround for script-like programs, that I sometimes use:


immutable int[int] aa;
static this() {
    aa = [1:15, 2: 7];
}
void main() {}


But when possible I try to avoid using static this.

Bye,
bearophile
Nov 08 2011
parent reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
I think this is a bug and i even recall seeing it in bugzilla, but i
don't have a link to it.

On Wed, Nov 9, 2011 at 10:43 AM, bearophile <bearophileHUGS lycos.com> wrot=
e:
 Is it possible to modify DMD to allow a program like:


 immutable int[int] aa =3D [1:15, 2: 7];
 void main() {}


 With the latest DMD it gives:
 test.d(1): Error: non-constant expression [1:15,2:7]

 Global immutable associative arrays are handy in script-like programs, I =
use them now and then in Python (despite they are not immutable).
 This is a workaround for script-like programs, that I sometimes use:


 immutable int[int] aa;
 static this() {
 =A0 =A0aa =3D [1:15, 2: 7];
 }
 void main() {}


 But when possible I try to avoid using static this.

 Bye,
 bearophile
Nov 09 2011
parent reply "mist" <none none.none> writes:
Googling is so good for necro thread resurrection.

What is idiomatic way in D to initialize static immutable AA to 
be accessible in CTFE at the same? static this() won't help here, 
unfortunately.

On Wednesday, 9 November 2011 at 10:44:08 UTC, Gor Gyolchanyan 
wrote:
 I think this is a bug and i even recall seeing it in bugzilla, 
 but i
 don't have a link to it.

 On Wed, Nov 9, 2011 at 10:43 AM, bearophile 
 <bearophileHUGS lycos.com> wrote:
 Is it possible to modify DMD to allow a program like:


 immutable int[int] aa = [1:15, 2: 7];
 void main() {}


 With the latest DMD it gives:
 test.d(1): Error: non-constant expression [1:15,2:7]

 Global immutable associative arrays are handy in script-like 
 programs, I use them now and then in Python (despite they are 
 not immutable).
 This is a workaround for script-like programs, that I 
 sometimes use:


 immutable int[int] aa;
 static this() {
    aa = [1:15, 2: 7];
 }
 void main() {}


 But when possible I try to avoid using static this.

 Bye,
 bearophile
Jan 24 2013
parent reply "mist" <none none.none> writes:
You know what? Screw idiomatic. Is there _any_ way to use a 
compile-time associative array literal?
Jan 24 2013
parent reply Artur Skawina <art.08.09 gmail.com> writes:
On 01/24/13 19:36, mist wrote:
 You know what? Screw idiomatic. Is there _any_ way to use a compile-time
associative array literal?
template ctAA(alias aal) { enum ctAA = aal; } enum aa = ctAA!(["one":1, "two":2]); int main() { enum x = aa["two"]; pragma(msg, x.stringof); return x; } artur
Jan 24 2013
next sibling parent reply "mist" <none none.none> writes:
Thank you. So simple and so illogical. Looking at your example I 
have zero understanding why AA literals just do not work at 
compile-time just out of the box.

On Thursday, 24 January 2013 at 19:30:27 UTC, Artur Skawina wrote:
 On 01/24/13 19:36, mist wrote:
 You know what? Screw idiomatic. Is there _any_ way to use a 
 compile-time associative array literal?
template ctAA(alias aal) { enum ctAA = aal; } enum aa = ctAA!(["one":1, "two":2]); int main() { enum x = aa["two"]; pragma(msg, x.stringof); return x; } artur
Jan 24 2013
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Jan 24, 2013 at 09:00:55PM +0100, mist wrote:
 Thank you. So simple and so illogical. Looking at your example I
 have zero understanding why AA literals just do not work at
 compile-time just out of the box.
[...] Because the current AA implementation sucks. You're welcome to try to improve it. I've tried before, but it's non-trivial because it's schizophrenically split across two places in druntime and sprinkled all over the compiler. Good luck. Should you succeed, you will have done a MAJOR favor to the entire D community (Andrei, for one, will be eternally grateful). T -- "A man's wife has more power over him than the state has." -- Ralph Emerson
Jan 24 2013
parent reply "mist" <none none.none> writes:
Sorry, no offense meant from my side :) I have followed your 
attempts on github and do not really feel in shape and competence 
to try to add anything there now. I simply could not understand 
why assignment to enum works and to immutable - not. Then 
remembered they enums are simply replaced with literals again and 
again every time when used and got it.

On Thursday, 24 January 2013 at 20:10:21 UTC, H. S. Teoh wrote:
 Because the current AA implementation sucks. You're welcome to 
 try to
 improve it. I've tried before, but it's non-trivial because it's
 schizophrenically split across two places in druntime and 
 sprinkled all
 over the compiler. Good luck. Should you succeed, you will have 
 done a
 MAJOR favor to the entire D community (Andrei, for one, will be
 eternally grateful).


 T
Jan 24 2013
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Jan 24, 2013 at 09:15:02PM +0100, mist wrote:
 Sorry, no offense meant from my side :) I have followed your
 attempts on github and do not really feel in shape and competence to
 try to add anything there now. I simply could not understand why
 assignment to enum works and to immutable - not. Then remembered
 they enums are simply replaced with literals again and again every
 time when used and got it.
I didn't take any offense. Just stating that AA's as currently implemented leaves a lot to be desired, and it's not going to improve unless *somebody* takes up the challenge to fix it.
 On Thursday, 24 January 2013 at 20:10:21 UTC, H. S. Teoh wrote:
Because the current AA implementation sucks. You're welcome to try to
improve it. I've tried before, but it's non-trivial because it's
schizophrenically split across two places in druntime and sprinkled
all over the compiler. Good luck. Should you succeed, you will have
done a MAJOR favor to the entire D community (Andrei, for one, will
be eternally grateful).
[...] T -- Elegant or ugly code as well as fine or rude sentences have something in common: they don't depend on the language. -- Luca De Vitis
Jan 24 2013
parent reply "mist" <none none.none> writes:
On Thursday, 24 January 2013 at 21:12:41 UTC, H. S. Teoh wrote:
 I didn't take any offense. Just stating that AA's as currently
 implemented leaves a lot to be desired, and it's not going to 
 improve
 unless *somebody* takes up the challenge to fix it.
Sure. But there are so many yummy bugs and issues hanging around D, it is so hard to chose among them :) By the way, could you provided a link to the your latest pull request on topic and related discussion? I remember seeing something in github notifications but can't find right now.
Jan 24 2013
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Jan 24, 2013 at 10:17:48PM +0100, mist wrote:
 On Thursday, 24 January 2013 at 21:12:41 UTC, H. S. Teoh wrote:
I didn't take any offense. Just stating that AA's as currently
implemented leaves a lot to be desired, and it's not going to improve
unless *somebody* takes up the challenge to fix it.
Sure. But there are so many yummy bugs and issues hanging around D, it is so hard to chose among them :) By the way, could you provided a link to the your latest pull request on topic and related discussion? I remember seeing something in github notifications but can't find right now.
I don't have a pull request for this. It's still a *long* way to go before it even gets close to being a pull request. But, you can look at the code here: https://github.com/quickfur/New-AA-implementation T -- The easy way is the wrong way, and the hard way is the stupid way. Pick one.
Jan 24 2013
prev sibling parent "mist" <none none.none> writes:
Ah, I get the trick - enum does not really hold an AA, it is just 
a copy-paste entity. Change the aa from enum to immutable - and 
it won't compile.
Jan 24 2013