On 6/17/12 1:38 AM, Philippe Sigaud wrote:
On Sat, Jun 16, 2012 at 9:22 PM, bearophile<[email protected]>  wrote:

I see. is that semantically different from this (beside being shorter)?

struct NoZero {
    int value;
    this(int x) { value = x; }
    alias value this;
    invariant() { assert(value != 0); }
}
void main() {
    auto a = NoZero(5);
    auto b = NoZero(0);
}

The invariant isn't invoked through alias this, it seems:

void main() {
    auto a = NoZero(5);
    a = 0; // compiles and runs happily
}

To avoid this, expose an rvalue via alias this, not an lvalue. Then implement opAssign etc.

Andrei

Reply via email to