Don wrote:
grauzone wrote:
and the code is more obfuscated. If you make an error in your
predicate, random funny things internal to the library implementation
could happen, and the compiler will spurt out indecipherable error
messages for random modules (I guess in this case, std.algorithm or
std.functional).
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).
template MixinThrowHelper(char[] comp)
{
// if your string mixin requires context, provide it here
// though this implies code duplication
mixin (comp);
enum MixinThrowHelper = true;
}
template Error(char[] msg)
{
pragma (msg, msg);
error;
}
static if (__traits (compiles, MixinThrowHelper!(comp)))
{
mixin (comp);
}
else
{
Error!("Bad predicate: " ~ comp);
}
The language changes required to give a perfect solution to this are
pretty small (just get static assert() to give the instantiation point
of each template which instantiated it, create a version of traits which
, given a string, tells you if it would compile if it were mixed in).
Then we get:
static assert(__traits(mixincompiles, comp), "Predicate: " ~ comp ~ "
does not compile");
mxin(comp);
That's certainly prettier.