www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Alternative to __traits(compiles)

Could we have an alternative version to __traits(compiles) that 
will give you the compiler errors?

Something like:
enum compilerErrors = __traits(compilerErrors, mixin(code));

// compilerErrors should be null if there are no errors.
static if (compilerErrors)
{
     pragma(msg, "Failed to compile code.");
     pragma(msg, compilerErrors);
     pragma(msg, code);
}
else
{
     mixin(code);
}

The closest to the above is doing the following with 
__traits(compiles):

mixin(code);

static if (!__traits(compiles, mixin(code))
{
     pragma(msg, "Failed to compile code.");
     pragma(msg, code);
}

But it just doesn't satisfy the order of message printing as the 
compiler errors are printed after the pragma(msg) calls. That way 
if you're compiling multiple pieces of long code it becomes kinda 
hard trying to read your error messages, where the first approach 
will let you print them in the order you need them.

It would be a really helpful thing in the combat of mixins vs 
error messages.

What do you think?
Oct 17 2017