templating opEquals/opCmp (e.g. for DSL/expression templates)
aliak
something at something.com
Tue Feb 12 07:34:28 UTC 2019
On Tuesday, 12 February 2019 at 04:15:34 UTC, 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
This looks super cool!!
Can a workaround for the opEquals be that a.opEquals(b) produces
a ProxyType and then ProxyType.opUnary gets you to where you want
to go?
More information about the Digitalmars-d
mailing list