ProtoObject and comparison for equality and ordering

H. S. Teoh hsteoh at quickfur.ath.cx
Wed May 15 18:00:31 UTC 2019


On Wed, May 15, 2019 at 10:37:10AM -0700, Walter Bright via Digitalmars-d wrote:
[...]
> I've seen no evidence that anyone would be interested in 4 state
> comparisons, and that's over two decades (yes, I implemented it in
> Digital Mars C++!).
> 
> I recommend we not waste time on this.

In one of my projects, I wrote a simple integer set type, which
obviously included a test for the subset relation.  Originally I wanted
to use opCmp for this purpose, but couldn't, because I was unaware of
the possibility of opCmp returning a float, and therefore couldn't
represent the incomparable state.

Now that I know about this possibility, I still decided against using
opCmp for the subset relation, because opCmp draws a sharp distinction
between strictly-less vs. less-or-equal, i.e.:

	x.opCmp(y) <= 0

requires deciding whether x was a strict subset of y so that opCmp knew
whether to return -1 or 0, but in the end that extra work is thrown away
anyway because the difference is ignored by the <= 0.  Computing the
difference between -1 and 0 was useless work, even though the definition
of opCmp required it.

So either way, implementing a custom .isSubsetOf() member function was a
far better solution than trying to make any use of opCmp's support for
partial orders.  And that's not to mention people's expectation when
seeing an expression like x < y in code; I'd wager 99.999% of people
would immediately think "linear order" rather than "partial order".
Changing the meaning of < to a partial order sounds like borderline
operator overloading abuse IMO, much as I like the concept of not
arbitrarily limiting user options.


T

-- 
MAS = Mana Ada Sistem?


More information about the Digitalmars-d mailing list