opDispatch and operator overloads

Timon Gehr timon.gehr at gmx.ch
Mon May 20 11:56:53 PDT 2013


On 05/20/2013 07:19 PM, Jonathan M Davis wrote:
> On Monday, May 20, 2013 17:15:32 John Colvin wrote:
>> Also, would it be a good idea to have free functions of all the
>> operators (opOpAssign etc...) for builtin types somewhere? It's
>> occasionally useful in generic wrappers.
>
> Why would this be useful? I think that it's just begging for trouble to be
> able to add stuff like "foo" + "bar" to the language via free functions. We
> don't _want_ that to be legal. That's why we have ~ in the first place. If you
> need to do something that you want to work with built-in types, and their
> operators don't do what you want, then just use a normal function rather than
> an operator. If you can't model your overloaded operator after what an
> operator does for the built-in types, it's arguably a bad choice to use an
> overloaded operator for that in the first place.
>
> - Jonathan M Davis
>

He just wants to be able to write:

assert(2.opBinary!"+"(3) == 5);

Which is entirely reasonable. This could be fixed by adding the 
corresponding built-in member function templates to the primitive types.
(Note that this would also disallow adding eg. "foo" + "bar" even when 
the UFCS/op-overload incompatibility is fixed.)


More information about the Digitalmars-d mailing list