Why is operator overloading like this in D?
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Sun Dec 10 03:23:32 PST 2006
> On the other hand, as there are many classes with which less or greater than
> comparisons (even equality tests) makes no sense, why opCmp becomes a "default"?
> Resulting in a requirement to make arguments of opCmp be of type Object when
That's to allow calling it if you only have an Object reference. This is
used in the implementation of associative arrays, and maybe some other
places in the standard library.
> ( Maybe I'm just feeling unused to test the type of objects inside the functions. :)
> If not overloaded, opEquals does a bit comparison on everything, doesn't it?
In structs, yes. In classes, Object.opEquals compares addresses (object
> And from the reference, I get: "If a struct has no opCmp() function declared for
> it, attempting to compare two structs is an error."
> And what does the default opCmp do with classes?
Throw an exception. (It used to compare addresses, but that was bad
because then the result can change between GC cycles if a moving
collector is implemented)
More information about the Digitalmars-d-learn