Purity (D2 standard libraries / object.d)

Robert Fraser fraserofthenight at gmail.com
Fri Jan 9 12:50:54 PST 2009


Walter Bright Wrote:

> Jason House wrote:
> > When considering the standard library changes, I realized that
> > object.d could change. I believe a pure opCmp or toString could break
> > user code with impure versions of those functions. Would that kind of
> > a change to object.d cause any real problems for D2 users?
> 
> As Andrei pointed out, the trouble with making the Object functions pure 
> is if you want to do an override that caches its value.

If you have:

class A {
    invariant final pure bool opEquals(invariant(Object) other) { ... }
}

int main() {
    auto a = cast(invariant) new A();
    auto b = cast(invariant) new A();
    if(a1 == a2) { return 0; } else { return 1; }
}

... will the compiler optimize it? Since A.opEquals() is final, there's no polymorphism, so it should be able to, but I don't know about the implementation.



More information about the Digitalmars-d mailing list