invariant/const class storage class

Yigal Chripun yigal100 at gmail.com
Wed Oct 1 06:44:28 PDT 2008


Walter Bright wrote:
> Simen Kjaeraas wrote:
>> Is this really correct behavior? How then are we supposed to
>> initialize our
>> invariant objects?
> 
> Currently, create them as mutable ones, then cast them to invariant.

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.



More information about the Digitalmars-d mailing list