Does `is` expression with template alias need fixing.

Dom Disc dominikus at scherkl.de
Tue Mar 21 08:43:59 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);
> }
> ```
>
> 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.

But if we make it work for struct, it will work with every type 
(with a little extra boilerplate):

```
template identity(int i) {
    struct wrapper { int w = i; }
    enum identity = w;
}

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

And with some new lowering we could even get rid of that 
boilerplate.
E.g. simply add the 'parent' property to the template parameters 
automatically if we detect that it is used in this way somewhere 
(but that may cost some compile time).


More information about the Digitalmars-d mailing list