On Thursday, 14 June 2018 at 17:07:09 UTC, Jonathan M Davis wrote:
Sure, it would save you a little bit of typing when you do
something like
auto foo = new Foo;
if makes it immutable for you, but it's at the cost of code
clarity.
Why should it even?
Isn't
immutable class C
{
int a;
}
the same as
class C
{
immutable
{
int a;
}
}
?
Does the following code clarify why an instance if immutable
struct HAS to be immutable while an instance of class does not
have to be immutable??
immutable struct S {}
immutable class C {}
void main()
{
S sa = S();
pragma(msg, typeof(sa)); // immutable(S)
S sb = S();
// sa = sb; // Error: cannot modify immutable expression sa
C ca = new C();
pragma(msg, typeof(ca)); // C
C cb = new C();
ca = cb; // works
}
Then the question would rather be why
S s = S(); // immutable(S)
does what it seems to be doing..?