Does `is` expression with template alias need fixing.

FeepingCreature feepingcreature at gmail.com
Tue Mar 21 18:03:42 UTC 2023


On Tuesday, 21 March 2023 at 17:21:00 UTC, Nick Treleaven wrote:
> On Tuesday, 21 March 2023 at 07:51:50 UTC, FeepingCreature 
> wrote:
>> This cannot ever work:
>>
>> ```
>> template identity(int i) { enum identity = i; }
>>
>> void foo() {
>>     enum five = identity!5;
>>     static assert(is(five == identity!y, int y) && y == 5);
>> }
>> ```
>
> Just to mention, even `is(five)` is false because five is not a 
> type. So the static assert above would be false even if the 
> pattern matching would succeed. (I have seen another D user say 
> they thought that `is` can test a type instance - it can't). To 
> match value patterns, another construct would be needed - 
> perhaps a `match` expression.
>
>> It cannot work because `five` does not lexically reference 
>> `identity` as a parent, because `five` is an int, and int is 
>> not a type that is or can be uniquely associated with the 
>> `identity` template.
>
> OK, and it also wouldn't work with `alias five = identity!5;`. 
> (If it wasn't an eponymous template, an inference expression 
> could work with that).

Well I mean sure, I handwaved a lot here. I think if I give an 
example that *could* work, it becomes even more obvious why it 
can't ever work:

```
alias identity(int i) = int;

void main() {
     alias five = identity!5;
     static assert(is(five == identity!y, int y) && y == 5);
}
```

Because `five` is a type, not an expression, and it's a type 
(`int`) that doesn't have a lexical parent.


More information about the Digitalmars-d mailing list