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

Reply via email to