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

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Feb 12 10:37:22 UTC 2019


On Monday, February 11, 2019 11:07:43 PM MST Nicholas Wilson via 
Digitalmars-d wrote:
> > They're supposed to emulate the built-in types with how normal
> > equality and the like works, not be used for creating new
> > syntax.
>
> Well the syntax is still the same, just the meaning is different.

Having the meaning be different is precisely the problem. It's using the
syntax for something _other_ than what it's intended for. It's just supposed
to be a way to have user-defined types emulate the operations of built-in
types. Walter has mentioned time and time again how he thinks that stuff
like << in C++ where they made the operators mean something completely
different than what they mean for built-in types was a big mistake, and
personally, I agree. If it were up to me, it wouldn't even be legal to have
opEquals return anything other than bool. In fact, I actually thought that
we'd fixed it at one point so that it _did_ have to be bool, but I saw a
post not that long ago where someone had an example that compiled where it
returned something else. So, I guess that the compiler isn't quite that
strict at the moment. Either way, I think that Walter has made it pretty
clear that overloaded operators are not intended for stuff like DSLs or
expression templates, and if you try to alter overloaded operators to make
them work better for expression templates or the like, I would fully expect
him to veto it. But I guess that we'll see his response soon enough if you
try to push this.

- Jonathan M Davis





More information about the Digitalmars-d mailing list