Operator overloading question
mist
none at none.none
Mon Jan 21 08:32:23 PST 2013
Do you want to discuss it? :) I have noticed that phobos style
guidelines favor constraints heavily over static asserts but this
exactly the example when I am uneasy about such choice: static
assert can both provide more user-friendly error message here and
remove some code duplication.
On Monday, 21 January 2013 at 12:27:35 UTC, bearophile wrote:
> Nathan M. Swan:
>
>> The correct keyword is "opBinary", not "opbinary".
>
> The compiler must give an error message easy to understand in
> similar wrong cases.
>
>
>> http://dpaste.1azy.net/b73ef2cd
>
> This is your code:
>
> Arithmetic opbinary(string op)(Arithmetic rhs)
> {
> static if(op == "+") return add(rhs);
> static if(op == "*") return mul(rhs);
> else static assert(0, "Operator "~op~" not implemented");
> }
>
>
> I like to add template constraints, where possible:
>
> Arithmetic opbinary(string op)(Arithmetic rhs)
> const pure nothrow if (op == "+" || op == "*")
>
> Bye,
> bearophile
More information about the Digitalmars-d-learn
mailing list