Why is object.opEquals not defined as pure?

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 19 01:34:19 PDT 2015


On Sunday, April 19, 2015 08:03:09 Muahmmad Adel via Digitalmars-d wrote:
> The following method definition is not possible because
> object.opEquals is not defined as pure.
>
> private pure ushort GetTypeHexadecimalValue(TypeInfo myType)
>   {
>       if (myType == typeid(byte))
>           return 0x01;
>       // some similar lines for other different basic types
>
>       throw new  Exception("Unknown type passed");
>   }
>
> What is the reason behind this? is opEquals modifying any global
> or object member variables?

If it were pure, then _every_ class' opEquals would have to be pure. The
same goes with const, @safe, and nothrow. And while some of those attributes
work for most implementations, they don't work for all - especially when
stuff like caching or lazy initialization gets involved. opEquals, opCmp,
toString, and toHash all have this problem. After much discussion on this in
the past, it was decided to remove all 4 of those functions from Object,
forcing base classes to implement them. Unlike in Java or C#, we have proper
templates, so there's really no need to use Object to call those functions,
and it should work to not have them on Object at all.

The problem is that that change hasn't been implemented yet. It was agreed
upon, and some work has been done towards it, but it's nowhere near
complete (e.g. the work required to templatize the free function opEquals
which calls opEquals on class instances has been blocked by compiler bugs).
So, we really haven't done what needs to be done to get this problem solved,
and until we do, the attribute situation on Object's built-in functions is a
bit of a mess.

The associated issues in bugzilla:

https://issues.dlang.org/show_bug.cgi?id=9769
https://issues.dlang.org/show_bug.cgi?id=9770
https://issues.dlang.org/show_bug.cgi?id=9771
https://issues.dlang.org/show_bug.cgi?id=9772

- Jonathan M Davis



More information about the Digitalmars-d mailing list