Does `is` expression with template alias need fixing.

FeepingCreature feepingcreature at gmail.com
Tue Mar 21 07:51:50 UTC 2023


On Saturday, 18 March 2023 at 01:15:35 UTC, Elfstone wrote:
>
> I'll perhaps try to read the compiler code someday, for now it 
> sounds to me like a matter of whether people want to fix the 
> bug or not, even if it involves fundamental redesign of the 
> frontend, or an extra pass to scan for unresolvable cases by 
> design.
>
> The compiler must at some point know that Foo is an alias, and 
> that a particular param of isInstanceOf will be expanded to 
> something inside `is(...)`. Store that info if not already, 
> report an error when Foo is passed to isInstanceOf.

To explain the mechanism:

The compiler knows what the template parameters are that are used 
to instantiate a `struct` that lives inside a template, because 
this information is stored *with the struct*. It can do this 
because - and only because! - the struct lives *uniquely* inside 
one particular template instantiation. That is, you can recover 
template parameters for precisely two cases, 
[structs](https://github.com/dlang/dmd/blob/v2.102.2/compiler/src/dmd/dtemplate.d#L4312) and [classes](https://github.com/dlang/dmd/blob/v2.102.2/compiler/src/dmd/dtemplate.d#L4450):

```
template Foo(T) {
   struct Foo { }
}
```

You can do this because, and *only* because, the lexical parent 
of `struct Foo` is `template Foo` with a given `T`. Template 
parameter recovery works for types that have the property of 
uniquely storing a lexical parent; it does not work for any types 
that don't have this property.

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.

What you want needs a fundamentally new language feature; 
something like typeclasses. Now there's an argument to be made to 
add typeclasses to the language, they would make ranges a lot 
more swanky for instance, but it's just not a matter of making 
template inference marginally more powerful.


More information about the Digitalmars-d mailing list