Safe to cast to immutable and return?
Timoses
timosesu at gmail.com
Thu Jul 5 13:14:36 UTC 2018
On Thursday, 5 July 2018 at 12:15:35 UTC, vit wrote:
>
> Try pure functions:
>
> class A {}
> A getA()pure @safe //pure whitout mutable parameters
> guarantees that function doesn't leak data.
> {
> A a;
> // .. do stuff with a
> // not leaking a to any functions
>
> // is this safe????
> return a;
> }
> A getA2(A x)pure @safe //pure with mutable param
> {
> A a;
> // .. do stuff with a
> // not leaking a to any functions
>
> // is this safe????
> return a;
> }
>
> void test()@safe{
> immutable(A) a1 = getA(); //ok
> immutable(A) a2 = getA2(null); //ok
> immutable(A) a3 = getA2(new A); //ok
> immutable(A) a4 = getA2(a3); //error in theory can leak
> }
Very nice!
I assume the @safe is not a must?
Good read:
https://dlang.org/spec/function.html#pure-functions
More information about the Digitalmars-d-learn
mailing list