It's basically Hindley-Milner.
jmh530
john.michael.hall at gmail.com
Fri Mar 24 13:17:40 UTC 2023
On Friday, 24 March 2023 at 09:44:28 UTC, FeepingCreature wrote:
> [snip]
>
> It's important to understand that IFTI does something
> *completely different* than normal type inference: it does
> *backwards* type inference. It's more similar to
> [Hindley-Milner] than D's normal "forward" type resolution.
>
I agree that it needs some kind of backwards type resolution.
Using the same initial example of the OP, consider something like
below (that's not valid D code currently).
```d
template Vec3(T) {
alias Vec3 = Matrix!(T, 3, 1);
template opResolveAlias(alias U)
{
static if(is(U == Matrix!(V, 3, 1), V))
alias opResolveAlias = Vec3!V;
else
static assert(0);
}
}
```
The `Vec3` function has another template alias associated with it
that handles the resolution, call it `opResolveAlias`. It can
take some type `U`, check if it matches the right hand side of
the original alias and then tells the compiler that it is a
`Vec3!V`. For simple aliases, the `opResolveAlias` could be
auto-generated by the compiler if not provided by the user.
Regardless, the important thing would be that `Vec3!V` is kept as
Vec3!V and not rewritten to `Matrix!(V, 3, 1)`. You could even
give it another notation, like `this!V` to make that effect
clearer. Then, when the compiler is doing template alias
resolution, it can tell that it is a template alias and do a
re-write.
So for instance, if you have `is(T == Vec3!S, S)` then you can
re-write it `is(Vec3.opResolveAlias!T == Vec3!S, S)`. Functions
might be a little more complicated, but the idea is the same: if
you pass some `T t` into a function `foo(S)(Vec3!S x) {}`, then
the resolution can similar check that `Vec3!S` is a template
alias and apply `Vec3.opResolveAlias!T` to any incoming `T`s.
More information about the Digitalmars-d
mailing list