Trying to do alias template deduction myself.

Elfstone elfstone at yeah.net
Mon Apr 3 12:56:10 UTC 2023


On Friday, 31 March 2023 at 05:19:48 UTC, Elfstone wrote:
> [..]

Some updates and thoughts.

The good new is, I have all the above cases working (except the 
one with tuple)! And even more complicated cases, such as:

```D
private struct SomeTemp(T, U, size_t N)
{
}

alias SomeAlias(X, Y, size_t C) = SomeTemp!(Y, X, C);

alias SomeOtherAlias(X, Y) = SomeAlias!(X, Y, 1);

void someOtherFoo(P, Q)(SomeOtherAlias!(Q, P) v)
{
     static assert(is(P == int));
     static assert(is(Q == float));
}

void main()
{
	someOtherFoo(SomeTemp!(float, int 1)()); // ok
	someOtherFoo(SomeTemp!(float, int, 3)()); // fails.
}
```

As I dig more into the compiler code, it's getting more obvious 
that this task is much tougher than I previously imagined. I 
cannot just "expand" the alias expression. It seems the 
"expansion" happens only during `templateInstanceSemantic` and I 
cannot reuse any of that with to-be-deduced arguments.

Now I take another approach: while matching `[U_, N_], Vec!(U_, 
N_)` to `Matrix!(float, 3, 1)`, I find the `AliasDeclaration` 
(alias Vec(U, N) = Matrix!(U, N, 1)) and call `deduceType` on 
`[U, N], Matrix!(U, N, 1)` - now it can produce [float, 3]. All I 
need to do is match [float, 3] to [U_, N_]. This also works 
recursively on a chain of aliases as long as it ends with a 
template declaration.

The potentional is likely the capability of original IFTI for 
alias template, but unfortunately I cannot reuse the existing 
matching code either (before I fully understand it, if ever). 
Still I figure it's better to write some simplistic matching with 
limit capability than writing a separate "expansion". It only 
takes several lines to do the basic matching anyway - the 
compiler will make sure the arguments really match later.

I'm almost sure my code works with bare type identifiers and 
values. I can probably support tuple soon. I'll figure out what I 
can do with fancy U:U*, U: A!U, etc.

I'll upload my code, when I feel more ... secure about the 
implementation, and because I coded during lunch break, I can't 
just upload it from work, maybe on weekends.






More information about the Digitalmars-d mailing list