GoingNative 2012 to be livestreamed tomorrow - part 2

Timon Gehr timon.gehr at gmx.ch
Fri Feb 10 05:04:00 PST 2012


On 02/10/2012 02:47 AM, bearophile wrote:
> Once this patch is applied:
> https://github.com/D-Programming-Language/dmd/pull/692
>
> you are able to write something like this, that isn't exceptionally nice looking, but it's useful (it's going to make Phobos code a bit more hairy, but the user is going to see some better error messages):
>
>
> template IsNumberWithError(T, string file, int line) {
>      enum bool IsNumberWithError = is( ...
>      static if (!IsNumberWithError)
>          __ctfeWriteln(file, "(", line, "): '", typeid(T), "' is not a number.");
> }
>
> double gsqrt(T)(T x) if (IsNumberWithError!(T, __FILE__, __LINE__)) { /*...*/ }
>

__ctfeWriteln is not required (it is not a declaration, therefore it 
probably won't work). pragma(msg, ...) can already be used for that purpose.

template IsNumberWithError(T, string file, int line) {
     enum bool IsNumberWithError = is( ...
     static if (!IsNumberWithError)
         pragma(msg,file, "(", line, "): '", T, "' is not a number.");
}

>
> An alternative is to give an else to the template constraints, but the error message is at the bottom of the function, making it not easy to find, so I don't like this syntax:
>
>
> int spam(T)(T x) if (IsFoo!T || IsBar!T) {
>      // ...
> } else {
>      __ctfeWriteln("'", typeid(T), "' is not Foo or Bar.");
> }
>
>

I like it. The else clause could perform arbitrary checks to make the 
error message as helpful as possible.




More information about the Digitalmars-d-announce mailing list