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

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Feb 12 04:42:39 UTC 2019


On Monday, February 11, 2019 9:15:34 PM MST Nicholas Wilson via Digitalmars-
d 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

Weren't D's overloaded operators designed specifically with the idea that
they _wouldn't_ support stuff like expression templates? They're supposed to
emulate the built-in types with how normal equality and the like works, not
be used for creating new syntax.

- Jonathan M Davis





More information about the Digitalmars-d mailing list