better compile-time messages for string functors

Max Samukha samukha at voliacable.com.removethis
Sun Feb 1 01:01:00 PST 2009


On Sat, 31 Jan 2009 11:28:07 -0800, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org> wrote:

>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

static assert should at least print the location of the instantiating
code or better the entire backtrace. This has been asked for several
times.



More information about the Digitalmars-d mailing list