ProtoObject and comparison for equality and ordering

Andrei Alexandrescu SeeWebsiteForEmail at erdani.com
Wed May 15 15:07:51 UTC 2019


On 5/14/19 10:35 PM, Mike Franklin wrote:
> On Wednesday, 15 May 2019 at 00:32:32 UTC, Andrei Alexandrescu wrote:
> 
>>> Although it would be much more work, perhaps what is needed is a new 
>>> type (e.g. `struct CmpResult`) with 4 immutable instances 
>>> representing each result and an `opCmp` and `opEquals` implementation 
>>> that does the right thing comparing against 0 or whatever else is 
>>> needed.  Yes, it's more complicated, but I think it would scale better.
>>
>> Not sure there's much to gain there. a < b is lowered to a.opCmp(b) < 
>> 0. So then... you define opCmp to return an instance of this:
>>
>> ---
>> import std.stdio;
>>
>> struct OverengineeredCmpResult {
>>     enum R { lt, eq, gt, ionno }
>>     private R payload;
>>     int opCmp(int alwaysZero) {
>>         writeln("b");
>>         return 0;
>>     }
>> }
>>
>> struct A {
>>     OverengineeredCmpResult opCmp(A rhs) {
>>         writeln("a");
>>         return OverengineeredCmpResult(OverengineeredCmpResult.R.ionno);
>>     }
>> }
>>
>> void main() {
>>     A a, b;
>>     if (a < b) {}
>> }
>> ---
>>
>> Much ado about nothing.
> 
> Cool! It actually looks much simpler than I imagined.

I don't think I made my point clear. Ultimately you're still relying on 
int. The entire code does nothing relevant. It was a joke in code form.


More information about the Digitalmars-d mailing list