invariant/const class storage class

Nicolas Sicard dransic at free.fr
Wed Oct 1 10:10:39 PDT 2008


Yigal Chripun a écrit :
> I understand casting as breaking the type system. shouldn't there be a
> way to create invariant objects without the cast?
> an invariant constructor seems a good way to solve this. any plans on
> adding such a feature to D2?
>
> I think it can work like this:
>
> class A {
>   this(params) {}   // [1]
>   invariant this(params) {}  // [2]
> }
>
> // create a mutable object - will use type [1] c-tors.
> auto a = new A(params);
>
> // create an invariant object - will use type [2] c-tors.
> invariant a = new A(params);
>
> // create a const object - will use type [1] or [2] c-tors.
> const a = new A(params);
>
> in the const case, if there are two c-tors which only differ by
> invariant-ness of the c-tor using either will create ambiguity and
> therefore should be a compile-time error.
>   

What should an invariant constructor do that a plain one don't, apart 
from specifying immutability of the instance?
This would mean you could create instances of different types from the 
same class, which is also kind of breaking the type system, isn't it?

But declaring a class invariant (for all instances) is something 
interesting.




More information about the Digitalmars-d mailing list