Make the constructor pure
and it can implicitly convert to any qualifiers.
this(int n) pure
{
i = n * 5;
}
auto i = new immutable ExampleClass(2);
auto m = new ExampleClass(3);
Documented here: http://dlang.org/class.html "If the constructor can create unique object (e.g. if it is pure), the object can be implicitly convertible to any qualifiers. "
BTW: the return value of other pure functions implicitly convert too.
// returns mutable...
char[] cool() pure {
return ['c', 'o', 'o', 'l'];
}
void main() {
char[] c = cool(); // so this obviously works
string i = cool(); // but since it is pure, this works too
}
Same principle at work there, it is unique so it can be assumed to be shared or immutable too.