ProtoObject and comparison for equality and ordering
SimonN
eiderdaus at gmail.com
Tue May 14 21:43:31 UTC 2019
On Tuesday, 14 May 2019 at 21:21:56 UTC, Mike Franklin wrote:
> Thinking about this a little more, why would the compiler even
> allow comparing two types that aren't comparable? Shouldn't
> that be a compiler error?
I agree that ==, <, > should not be allowed for a bare
ProtoObject and that violation should be a compilation error,
explaining what interface you have to implement to get comparison.
The point of ProtoObject is to fix the problems of Object. It's
questionable that class Object allows comparison at compile time,
but immediately throws at runtime:
int opCmp(Object o)
{
throw new Exception("need opCmp for class " ~
typeid(this).name);
}
This brought a latent bug in my code: I put a class that didn't
override opCmp into an associative array. All goes well as long
as the hashes were different. Then, months later, the code
crashes when two hashes collide and opCmp is called.
Certainly that bug was my fault, but maybe we can prevent such
bugs at compile time with ProtoObject?
-- Simon
More information about the Digitalmars-d
mailing list