ProtoObject and comparison for equality and ordering

H. S. Teoh hsteoh at quickfur.ath.cx
Tue May 14 23:13:23 UTC 2019


On Tue, May 14, 2019 at 03:47:18PM -0600, Jonathan M Davis via Digitalmars-d wrote:
> On Tuesday, May 14, 2019 3:21:56 PM MDT Mike Franklin via Digitalmars-d 
> 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?
> 
> The issue is being able to reproduce the behavior of comparing
> floating point types when one of them is NaN. That design of floating
> point comparison does make some sense with regards to floating points
> but seriously complicates things for overloading opCmp. In general, it
> really doesn't make sense to return anything other than an integral
> value, but if you want to be able to create a type that wraps floats
> (e.g. Nullable!float) or emulates them in some manner, then you need
> more flexibility.
[...]

Moreover, Andrei has mentioned before that opCmp can technically be used
for implementing partial orders. I had thought otherwise in the past,
because I only considered opCmp that returns int. However, if opCmp is
allowed to return float, then you can return float.nan for the
incomparable case (e.g., two sets that are not subsets of each other)
and thus achieve a non-linear partial ordering, such as the subset
relation.

Whether or not this is a *good* way of implementing the subset operation
is a different question, of course. If we restricted opCmp to only
linear orderings, then this wouldn't be an issue.


T

-- 
Curiosity kills the cat. Moral: don't be the cat.


More information about the Digitalmars-d mailing list