Discussion Thread: DIP 1042--ProtoObject--Community Review Round 1

Mike Parker aldacron at gmail.com
Thu Jan 13 00:57:08 UTC 2022


On Thursday, 13 January 2022 at 00:43:52 UTC, Mike Parker wrote:
> 

 From Paul Backus:

On Wednesday, 12 January 2022 at 20:26:17 UTC, Dom DiSc wrote:
> On Monday, 10 January 2022 at 14:44:22 UTC, Elronnd wrote:
>> 'cmp' should not return -1 when it can not compare the 
>> specified classes.  It should not pretend to have a 
>> significant result when it does not.
>
> Yep. 'cmp' should return a float (using 4 values: -1, 0, 1 and 
> NaN). This allows to return NaN if some things are not 
> comparable. (even better would be if D had a native 2bit type 
> with exactly those 4 values)

Alternatively: we don't define a global "Comparable" interface at
all, and the compiler handles opCmp on classes the same way it
does for structs, using the static type of the object to
determine which overload to call.

That way, you can have opCmp return int if your class is totally
ordered, or float if it's partially ordered. And you can also
choose whatever function attributes you like.

If you want to write a function that takes "anything comparable"
as an argument, and you don't want to use templates, you can use
a type erasure library like Atila Neves's tardy [1] to convert
objects with different opCmp overloads to a common type that uses
dynamic dispatch under the hood. There is no need to have all of
them inherit from a common interface.

[1] https://code.dlang.org/packages/tardy



More information about the Digitalmars-d mailing list