__traits(compiles, ...) with syntax errors
Steven Schveighoffer
schveiguy at gmail.com
Mon Jan 14 21:52:13 UTC 2019
On 1/14/19 4:47 PM, Steven Schveighoffer wrote:
> How many times have you written something like:
>
> void foo(T)(T t) if (__traits(compiles, t.bar())) // or is(typeof(t.bar()))
> {
> t.bar();
> }
>
> And somehow, somewhere, this isn't called? Then you remove the
> constraint, and find that there's a syntax error in the bar template
> function (like a missing semicolon).
>
> I often times, to avoid this, simply just use __traits(hasMember, T,
> "bar"), and live with the consequences.
>
> I'd love to have something like __traits(compiles), but have it be an
> error if the syntax is incorrect.
>
> In other words, the constraint above only fails if there is a missing
> symbol, or incorrect type conversion, not that there is a stupid syntax
> error that would NEVER compile.
>
> Ironically, it would be something like compilesOrSyntaxError.
Another option is to have __traits(compiles) spit out an error type that
has truthiness if it's not an error (for backwards compatibility), then
you could something like:
if(__traits(compiles, ...) != COMPILER_ERROR.SYNTAX)
-Steve
More information about the Digitalmars-d
mailing list