Difference between "can call" and "can compile"

FeepingCreature feepingcreature at gmail.com
Mon Sep 7 15:48:57 UTC 2020


On Monday, 7 September 2020 at 15:24:23 UTC, Stefan Koch wrote:
> On Monday, 7 September 2020 at 15:08:42 UTC, FeepingCreature 
> wrote:
>> On Monday, 7 September 2020 at 14:57:24 UTC, Steven 
>> Schveighoffer wrote:
>>> I was wondering, would it be a useful addition to have a way 
>>> to say "can call" instead of "can compile"? In other words, 
>>> this has a valid template IFTI match, regardless of whether 
>>> it compiles or not. In which case, you can distinguish 
>>> mismatch errors from implementation errors.
>>>
>>> Thoughts?
>>>
>>> -Steve
>>
>> Yes yes yes yes yes *yes yes*!
>>
>> Please!!!!!
>>
>> This would make template errors so unbelievably much better.
>
> I can see the appeal of the feature.
>
> Let me think about how difficult that would be to implement.

That would be so, so awesome.

Let me clarify how important this is to me: I actually had a 
section at the end of my proposed DConf Online talk where I was 
gonna beg the devs to add this exact feature. In cases where you 
want a template function to match, but *maybe* will be fine if it 
doesn't match, so you have to keep going even if it doesn't 
__traits(compiles), this can be the difference between half an 
hour of debugging and instantly being told the (usually trivial) 
problem.

To explain:

You can pass `serialized`'s decode functions a helper function 
that can be used to decode problematic leaf types like `SysTime` 
or UUID in a protocol-specific fashion. However, if that helper 
doesn't match, we still want to keep going and try our default 
decode logic.

So how do we check if a helper matches? For instance, the helper 
may be

SysTime decode(T : SysTime)(const string attribute) { return 
SysTime.fromISOExtString(attribute); }

And then internally I do static if (__traits(compiles, 
decode!SysTime)).

But say that I forgot to import std.datetime. Then 
__traits(compiles) will return false and I will have no idea what 
happened. What I actually *want* is __traits(canInstantiate, 
decode, SysTime) - check if the template constraints can be 
satisfied enough to find an unambiguous template match. If I then 
try to instantiate the template and the compiler raises an error, 
that's okay! That's an error I want. I want that error. Give me 
that error. Please. :)

(Note that this doesn't *exactly* reduce to a function call. An 
important part here is that I don't know what the function will 
be called with; I have to instantiate the function template so 
that I can check what parameters it requires, which I'll then 
recursively try to decode.)


More information about the Digitalmars-d mailing list