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

Timon Gehr timon.gehr at gmx.ch
Tue Feb 12 07:39:29 UTC 2019


On 12.02.19 05:15, Nicholas Wilson wrote:
> 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

Ideally those would both be cases of opBinary, but I guess your proposal 
has better backwards compatibility.


More information about the Digitalmars-d mailing list