Class qualifier vs struct qualifier

Timoses timosesu at gmail.com
Sat Jun 16 07:13:28 UTC 2018


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..?


More information about the Digitalmars-d-learn mailing list