Does `is` expression with template alias need fixing.

Nick Treleaven nick at geany.org
Tue Mar 21 17:21:00 UTC 2023


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).


More information about the Digitalmars-d mailing list