Don wrote:
Not necessarily. Andrei can just add:

static if(__traits(compiles, mixin(comp) )) {
  mixin(comp);
} else {
   // static assert is a bit broken,
   // better to do it this way to provide a backtrace.
   pragma(msg, "Bad predicate: " ~ comp);
   deliberate_error_message; // t
}

into std.functional. Which will generate a very controlled error message.
(Doesn't work if the predicate contains mismatched parentheses, but that's fixable).

I tried the above and didn't quite work. The deliberate_error_message would trigger an error even for valid expressions. What I have now is this:

... inside binaryFunction ...
enum testAsExpression = "{"~ElementType1.stringof
     ~" "~parm1Name~"; "~ElementType2.stringof
     ~" "~parm2Name~"; return ("~fun~");}()";
static if (__traits(compiles, mixin(testAsExpression)))
{
    enum string code = "return (" ~ fun ~ ");";
    alias typeof(mixin(testAsExpression)) ReturnType;
}
else
{
    static assert(
        false,
        "Bad binary function: " ~ fun ~ " for types "
        ~ ElementType1.stringof ~ " and "
        ~ ElementType2.stringof ~
        ". You need to use an expression using symbols "
        ~parm1Name~" and "~parm2Name~".");
}

This seems to work well but does not provide the line in the instantiating code. Any ideas on how to do it better?


Andrei

Reply via email to