Difference between "can call" and "can compile"

Steven Schveighoffer schveiguy at gmail.com
Wed Sep 16 13:04:27 UTC 2020


On 9/16/20 1:58 AM, WebFreak001 wrote:
> On Tuesday, 15 September 2020 at 19:12:04 UTC, Steven Schveighoffer wrote:
>> On 9/15/20 10:18 AM, WebFreak001 wrote:
>>> [...]
>>
>> But that's the issue. If I know what small set of types should work, 
>> then I can just write overloads for those types.
>>
>> It's when I don't know what type is going to be passed in.
>>
>> I could probably switch to using "hasMember" in this use case, which 
>> is likely to be correct for all purposes that I have. It's just not 
>> applicable to a public library, which might use things other than 
>> members.
>>
> 
> huh where did you get that from? My proposed solution there just checks 
> if you can call a function with the given arguments only if it's passing 
> all the template constraints.

Oh, I misread what you were doing. I assumed you were using traits to 
get parameter information, and I didn't bother reading very well. Sorry!

Indeed, this is a close solution. You would need to be worried about 
possible import issues (or overloads). Plus you might have unused 
functions that are not called.

I'd also want to directly call the function instead. I may want to 
actually verify the linkage is correct.

Something like:

template doppleganger(alias fun)
{
     mixin("private void doppleganger" ~ def[def.indexOf('(' .. $] ~ " {}");
}

static assert(__traits(compiles, fun!foo(args)));

Need a way to pass templates. But it does seem doable. As you point out, 
the stringof thing is pretty sketchy.

But yes, this is pretty much what I would want to have. Just more of a 
solid feature than a hack ;)

-Steve


More information about the Digitalmars-d mailing list