__traits(compiles, ...) with syntax errors
Steven Schveighoffer
schveiguy at gmail.com
Mon Jan 14 22:26:11 UTC 2019
On 1/14/19 4:57 PM, Paul Backus wrote:
> On Monday, 14 January 2019 at 21:47:40 UTC, 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).
>
> Ideally, you would catch this in the unit tests for `bar`. You are
> writing unit tests, aren't you? ;)
It's not always that simple. Sometimes bar is part of a hook or
something to a complex system, and you expect it to be called one way,
and it's called another way (or the lack of ability to call it changes
how the system behaves).
Do you unit test all your calls to front(), popFront or empty
individually? I generally don't, I just put it in as a range, and see if
that works.
Then I get an error like "this isn't a range", and it's not super-helpful.
Or worse, I get a "I can't call any of these 10 template overloads, each
with a 5-term constraint"
-Steve
More information about the Digitalmars-d
mailing list