On Saturday, 29 November 2014 at 09:41:00 UTC, jostly wrote:
I can't find a way to use a pure constructor to create both mutable and immutable instances of the same class, when one of the fields I assign is a string.
[...]
The question is: am I missing something that would make it possible to use a pure constructor in this case, or is it simply not possible?

I ran some tests, as far as I can tell, they should all work:

mixin template test(T)
{
     class C
     {
         T value;
         this(T value_) pure {this.value = value_;}
     }

     static assert(is(typeof({auto c = new C(T.init);})));

     static if(!is(typeof(new immutable C(T.init))))
         pragma(msg, T, ": immutable construction fails");

     static if(!is(typeof({immutable c = new C(T.init);})))
         pragma(msg, T, ": unique construction fails");
}

/* No indirections (and no (d/w)char): all fine */
mixin test!int;
mixin test!(int[3]);

/* With indirections:
immutable construction fails
unique construction works */
mixin test!string;
mixin test!(immutable int[]);
mixin test!(immutable Object);
mixin test!(immutable int*);

/* No indirections, but (d/w)char:
immutable construction works
unique construction fails
Wat. */
mixin test!dchar;
mixin test!wchar;
mixin test!char;
mixin test!(dchar[3]);

Reply via email to