templating opEquals/opCmp (e.g. for DSL/expression templates)

Nicholas Wilson iamthewilsonator at hotmail.com
Tue Feb 12 04:15:34 UTC 2019


With opBinary and opUnary it is possible to create types that 
span the binary and unary operators. However opEquals can only be 
used to types that compare for equality not inequality, since a 
== b -> a.opEquals(b) can return whatever type it likes but a == 
b -> !(a.opEquals(b)) returns a bool. opCmp can't be used at all 
since a <=> b -> a.opCmp(b) <=> 0 which evaluates to a bool.

I propose that in addition to the current (non-template) forms of 
opCmp/opEquals

struct A { bool opEquals(A rhs); int opCmp(A rhs); }

we allow

struct B
{
     T opEquals(string op)(B rhs); // op is "<" "<=" etc.
     T opCmp(string op)(B rhs);    // op is "==" "!="
}

where T is any arbitrary type.

see also 
https://github.com/k3kaimu/dranges/blob/master/source/dranges/_lambda.d


More information about the Digitalmars-d mailing list