opEquals default behaviour - poorly documented or am I missing something?
MichaelZ via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Nov 18 06:05:17 PST 2015
On Tuesday, 17 November 2015 at 19:44:36 UTC, Ali Çehreli wrote:
> On 11/17/2015 12:40 AM, MichaelZ wrote:
> > In http://dlang.org/operatoroverloading.html#eqcmp it is
> stated that
> >
> > "If opEquals is not specified, the compiler provides a
> default version
> > that does member-wise comparison."
> >
> > However, doesn't this only apply to structs, and not objects?
>
> Correct. The behavior for class objects is the following
> algorithm on the same page:
>
> http://dlang.org/operatoroverloading.html#equals
>
> This one:
>
> bool opEquals(Object a, Object b)
> {
> if (a is b) return true;
> if (a is null || b is null) return false;
> if (typeid(a) == typeid(b)) return a.opEquals(b);
> return a.opEquals(b) && b.opEquals(a);
> }
Sure, but that defers to the a.opEquals / b.opEquals in many the
interesting cases :) Which comes back to the original point:
that I feel the documentation I quoted is at least easily
misunderstood, if not straight out wrong.
More information about the Digitalmars-d-learn
mailing list