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

Ali via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 14 04:02:16 PST 2016


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.

What's the deal with static immutable associative arrays and D 
anyway? Is there some kind of roadmap somewhere or bugs that need 
to be fixed before they work properly in D? I'm confused because 
it seems like the data actually gets inside the struct object 
that has an AA as a member, but then the compiler just decides 
"nah, just kidding, this ain't actually ok. bye".

>
> I moved the frequencies to 'data' and hte following worked.

Yeah that is indeed the workaround I mentioned and hence the need 
for the pointer to room problem I was having :p This does work. 
But I guess I'm just looking for a more ideal solution.


Thanks for all the help so far! :)



More information about the Digitalmars-d-learn mailing list