initializing const maps with value types having aliasing

Dan dbdavidson at yahoo.com
Wed Mar 20 12:15:46 PDT 2013


On Wednesday, 20 March 2013 at 19:01:27 UTC, Jonathan M Davis 
wrote:
> Why are you casting? The cast shouldn't be necessary, because 
> you're doing the
> initialization inside a static constructor.

Without it I get:
Error: mutable method cmap.S.__postblit is not callable using a 
const object
Error: cannot modify struct this Slot with immutable members


> If you had problems, I'd expect it
> to be that AAs don't work properly when const (I know that 
> there are issues
> when they're immutable) or that you can't insert elements into 
> a const or
> immutable AA (which you'll never be able to do). But what 
> you're doing here
> should work just fine without the cast. Assuming that AAs 
> worked with const or
> immutable correctly, then it would be normal to do something 
> like
>
> immutable int[string] aa;
>
> static this()
> {
>  int[string] temp;
>  temp["foo"] = 7;
>  temp["blah"] = 12;
>  aa = assumeUnique(temp);
> }

For now it seems the cast is necessary - so as long as it is safe.

I am not using 'immutable S[string]aa', but it would be 
interesting to see how that could be initialized. So, how to 
initialize aa. Does assumeUnique work for associative arrays?
------------------
import std.exception;

struct S {
   this(this) { x = x.dup; }
   char[] x;
}

immutable S[string] aa;

static this() {
    // now what
}

Thakns
Dan


More information about the Digitalmars-d-learn mailing list