Does `is` expression with template alias need fixing.

jmh530 john.michael.hall at gmail.com
Thu Mar 16 12:51:39 UTC 2023


On Thursday, 16 March 2023 at 01:57:34 UTC, Elfstone wrote:
> [snip]
>
> Can't the compiler also rewrite or expand `Vec3!S` to 
> `Matrix!(S, 3, 1)` then comfortably evaluate
> ```D
> is(Matrix!(float, 3, 1) == Matrix!(S, 3, 1), S) // true
> ```
> ?
>
> It looks easier than the reverse approach, but again I don't 
> know anything about D compiler.
>
> It'd be better if the compiler would just report an error: 
> "don't you ever put template alias in `is(...)`". The current 
> behaviour is confusing and of zero use to anyone.

DIP1023 was all about resolving template aliases. The compiler 
currently treats `Vec3!S` and `Matrix!(S, 3, 1)` as different 
things for the purpose of calling functions. DIP1023 tries to 
re-write `Vec!S` into `Matrix!(S, 3, 1)`.

The problem is that

```d
alias Vec3(S) = Matrix!(S, 3, 1);
```

is actually shorthand for

```d
template Vec3(S) {
     alias Vec3 = Matrix!(S, 3, 1);
}
```

and it turns out that this is very powerful. Not that you'd want 
to, but you could just as easily write

```d
template Vec3_alt(S) {
     static if (!is(S == int))
         alias Vec3_alt = Matrix!(S, 3, 1);
     else
         alias Vec3_alt = real;
}
```

In this case, how do you resolve `Vec3!S`? It's not as simple as 
just re-writing it to `Matrix!(S, 3, 1)`.

The solution in DIP1023 only works for a limited--albeit 
common--subset of the functionality that D supports for template 
aliases, so it is essentially special casing for template alias 
deduction. That and running up against the question of "why not 
just use template constraints", it struggled to gain support.


More information about the Digitalmars-d mailing list