View source code
Display the source code in object.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function object.opEquals

Returns true if lhs and rhs are equal.

bool opEquals (
  const(Object) lhs,
  const(Object) rhs
);

Example

If aliased to the same object or both null => equal

class F { int flag; this(int flag) { this.flag = flag; } }

F f;
assert(f == f); // both null
f = new F(1);
assert(f == f); // both aliased to the same object

Example

If either is null => non-equal

class F { int flag; this(int flag) { this.flag = flag; } }
F f;
assert(!(new F(0) == f));
assert(!(f == new F(0)));

Example

If same exact type => one call to method opEquals

class F
{
    int flag;

    this(int flag)
    {
        this.flag = flag;
    }

    override bool opEquals(const Object o)
    {
        return flag == (cast(F) o).flag;
    }
}

F f;
writeln(new F(0)); // new F(0)
assert(!(new F(0) == new F(1)));

Example

General case => symmetric calls to method opEquals

int fEquals, gEquals;

class Base
{
    int flag;
    this(int flag)
    {
        this.flag = flag;
    }
}

class F : Base
{
    this(int flag) { super(flag); }

    override bool opEquals(const Object o)
    {
        fEquals++;
        return flag == (cast(Base) o).flag;
    }
}

class G : Base
{
    this(int flag) { super(flag); }

    override bool opEquals(const Object o)
    {
        gEquals++;
        return flag == (cast(Base) o).flag;
    }
}

writeln(new F(1)); // new G(1)
writeln(fEquals); // 1
writeln(gEquals); // 1

Authors

Walter Bright, Sean Kelly

License

Boost License 1.0.