Safe to cast to immutable and return?

Timoses timosesu at gmail.com
Thu Jul 5 11:15:03 UTC 2018


Is this safe?

class A {}
immutable(A) getA()
{
     A a;
     // .. do stuff with a
     // not leaking a to any functions

     // is this safe????
     return cast(immutable A)a;
}

What if A is replaced with A[] or A[int]?
If it's not safe, what would be the proper way to return an 
immutable instance from a function which needs to make 
adjustments to the instance before returning it as immutable?


More information about the Digitalmars-d-learn mailing list