On Wednesday, 3 June 2015 at 09:10:22 UTC, David Monagle wrote:
What I was looking for is a more elegant way of defining those secondary functions. Originally I was hoping I could do something like:

enum shouldEqual(E, V) = shouldValue((e, v) => e == v, "equal", E, V);

Here you go:
----
template shouldValue(alias operation, string description)
{
bool shouldValue(E, V)(lazy E expression, V value, string name="Value",
        string file = __FILE__, typeof(__LINE__) line = __LINE__)
    {
        if (operation(expression, value)) return true;
throw new Exception(format("%s should %s %s, but was actually %s",
            name, description, value, expression), file, line);
    }
}

alias shouldEqual = shouldValue!((e, v) => e == v, "equal");
alias shouldBeGreaterThan = shouldValue!((e, v) => e > v, "be greater than");
----

The trick is to split `shouldValue` into two nested templates.

The outer template has those parameters that cannot be deduced: `operation` and `description`.

The inner template has the parameters that should be deduced: `E` and `V`.

Reply via email to