Feature request: __traits(canInstantiate), like __traits(compiles) but without suppressing syntax errors

user1234 user1234 at 1234.de
Wed Jan 22 18:24:17 UTC 2020


On Tuesday, 21 January 2020 at 22:32:45 UTC, Steven Schveighoffer 
wrote:
> On 1/18/20 10:21 AM, FeepingCreature wrote:
>> Yes it can!
>> 
>> D already has support for exactly this kind of thing, with 
>> std.traits and `if()` constraints. If you have a metafunction 
>> that tries to see if "an alias is usable with string", and it 
>> checks that with __traits(canInstantiateCall, foo, 
>> string.init) or whatever, then you *should* get a syntax error 
>> even in the "return x + 5" case. If you want to avoid that, we 
>> don't need language changes, you just need to use the *already 
>> existing* mechanism of "if (isNumeric!T)".
>
> Wait, I think I get it.
>
> You want something that says if the constraints allow the 
> instantiation to proceed, then instantiate, and display the 
> errors as generated. If not, then return false.
>
> [...]
>
> -Steve

Yes and there's an API for that
https://github.com/dlang/dmd/blob/ab2ea3880a144957811d3a8f7c63dfab0e351202/src/dmd/dtemplate.d#L762


More information about the Digitalmars-d mailing list