Accessing members through pointers to structs (also, CTFE associative arrays)

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 15 11:30:08 PST 2016


On 12/14/2016 04:02 AM, Ali wrote:
 > On Tuesday, 13 December 2016 at 23:29:31 UTC, Ali Çehreli wrote:
 >> On 12/13/2016 01:36 PM, Ali wrote:
 >>
 >>> Now about that second part of my problem ....
 >>
 >> I'm not entirely sure whether this should work but I think the problem
 >> is with mutating the 'frequencies' member of an immutable element of
 >> 'rooms'. The error message means that those non-const expressions
 >> cannot be shared by a member of an immutable AA.
 >
 > I'm not sure I fully follow here. Because the variable in the parse
 > function is not immutable and the frequencies member is not "created"
 > yet, so to say. So after I create the frequencies object, I assign it to
 > the member of a Room object. The following illustrates this more clearly
 > I think:
 >
 > struct A { int x; }
 > struct B { int[char] x; }
 > auto f1() {
 >   int x;
 >   x = 3;
 >   return A(x);
 > }
 >
 > auto f2() {
 >   int[char] x;
 >   x['A'] = 2;
 >   return B(x);
 > }
 >
 > static immutable a = f1();
 > static immutable b = f2();
 >
 > pragma(msg, a);
 > pragma(msg, b);
 >
 > This fails to compile with "Error: non-constant expression ['A':2]".
 > But, the pragma(msg) prints out the correct information for both a and b.

Yeah, I think the compiler is confused because the function is called in 
a non-const context during the initialization of an immutable object.

I would open an issue:

   https://issues.dlang.org/enter_bug.cgi?product=D

Ali



More information about the Digitalmars-d-learn mailing list