ProtoObject and comparison for equality and ordering
Mike Franklin
slavo5150 at yahoo.com
Wed May 15 02:35:55 UTC 2019
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.
Mike
More information about the Digitalmars-d
mailing list