why are opCmp and opEquals not pure.

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Thu Sep 17 12:26:19 PDT 2015


On Thu, Sep 17, 2015 at 06:37:04PM +0000, Stefan Koch via Digitalmars-d wrote:
> Hi,
> is there any reason why opCmp and opEquals are not pure ?
> I would argue it is very counter-intuitive to mutate any state when
> comparing objects.
[...]

The way I understand it, this is a historical accident: opCmp and
opEquals date back to the days before the const system was introduced to
D, so they were never originally annotated. When the const system was
introduced, a good amount of code is already using opCmp and opEquals,
and some of them may mutate state (e.g., cache the result of previous
comparisons in the object if the comparison operation is expensive), so
adding the annotations would break existing code.

At the time, a lot of the affected code was in Phobos, where there was a
giant tangle of dependencies where changing the const annotation on a
single function would percolate to almost half of the entire Phobos (if
not more), breaking many other seemingly-unrelated things (and
introducing potential for breakage of user code that use those things),
so it was difficult to make the transition.

Since that time, there has been talk of removing opCmp and opEquals from
Object altogether, but so far we haven't managed to do this yet.


T

-- 
Acid falls with the rain; with love comes the pain.


More information about the Digitalmars-d mailing list