What am I missing? Pure constructor behaves differently when assigning string member

2014-11-29 Thread jostly via Digitalmars-d-learn
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?


Re: What am I missing? Pure constructor behaves differently when assigning string member

2014-11-29 Thread jostly via Digitalmars-d-learn

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.


After poking around a bit, I believe it is caused by issue #10012 
https://issues.dlang.org/show_bug.cgi?id=10012 :


This is current dmd implementation limitation. In complex cases 
dmd cannot detect that the constructor generates unique object.


So my question then becomes, how can I generate an unique object 
from the string? I tried using .idup on the incoming string, but 
that didn't help. Or is it simply still a problem of _detecting_ 
it for the compiler?