On 02/08/2012 07:30 PM, Artur Skawina wrote:
On 02/08/12 18:24, Timon Gehr wrote:

Open issue:

void main(){
     unique x = new C;
     pragma(msg, typeof(x.z)); // ???
     auto y = x.z; // error?
     immutable z = x;
}

x.z is not necessarily unique, but assigning it to y breaks the guarantees of 
the unique qualifier of x.

This is why treating "unique" as a static storage class doesn't really work. One
of the two last lines above would have to be disallowed,


Actually that is not true. It could type check, because y is not alive after x has been cast to immutable.

The problem shows here:

void main(){
    unique x = new C;
    auto y = x.z;
    immutable z = x;
    foo(y,z);
}


and that reduces its usefulness dramatically.

Can you show me an example of its use?

The "auto" assignment either has to be illegal, or
trigger a mutation, eg to immutable. I don't think having the compiler keep
track of all possible relations would be a practical solution, even if 
theoretically
possible.

I think it would be practical enough.

typeof(C.z) global;

void main(){
    unique x = new C;
    auto y = x.z;
    static assert(!is(typeof(y)==immutable));
    static assert(!is(typeof({global = y;}));
    immutable z = x;
    static assert(is(typeof(y)==immutable));
    static assert(is(typeof(x)==immutable);
    foo(y,z);
}


For those not reading D.learn - unique helps in other cases too:
http://lists.puremagic.com/pipermail/digitalmars-d-learn/2012-February/029547.html

artur

Reply via email to