[ create a new paste ] login | about

Link: http://codepad.org/tOHnTObl    [ raw code | fork ]

D, pasted on Oct 23:
class UncomparableException: Exception {
    this() {
        super("");
    }
    this(string msg) {
        super(msg);
    }
}


class C1 { // WRONG?
    int x;
    this(int x) { this.x = x; }

    int opEquals(Object o) {
        auto oc = cast(typeof(this))o;
        if (oc is null)
            return 0;
        else
            return this.x == oc.x;
    }

    int opCmp(Object o) {
        // ...
    }
}


class C2 { // RIGHT?
    int x;
    this(int x) { this.x = x; }

    int opEquals(Object o) {
        auto oc = cast(typeof(this))o;
        if (oc is null)
            throw new UncomparableException();
        else
            return x == oc.x;
    }

    int opCmp(Object o) {
        auto oc = cast(typeof(this))o;
        if (oc is null)
            throw new UncomparableException();
        else
            return this.x - oc.x;
    }
}

void main() {}


Create a new paste based on this one


Comments: