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.

This works fine:
        class A
        {
                int value;
                
                this(int value_) pure
                {
                        this.value = value_;
                }       
        }
        
        auto a_mutable = new A(1);
        auto a_immutable = new immutable A(2);

But if I change the field to a string, I get a compilation error:
        class B
        {
                string value;
                
                this(string value_) pure
                {
                        this.value = value_;
                }       
        }
        
        auto b_mutable = new B("foo");
        auto b_immutable = new immutable B("bar");

giving a compilation error for the last row:

Error: mutable method B.this is not callable using a immutable object

forcing me to use two separate constructors, which works fine:
        class B
        {
                string value;
                
                this(string value_)
                {
                        this.value = value_;
                }       

                this(string value_) immutable
                {
                        this.value = value_;
                }       
        }

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?

Reply via email to