Does `is` expression with template alias need fixing.

jmh530 john.michael.hall at gmail.com
Wed Mar 15 13:07:53 UTC 2023


On Wednesday, 15 March 2023 at 06:47:38 UTC, Elfstone wrote:
> [snip]
> void main()
> {
>     import std.stdio;
>
>     Vec3!float v;
>     writeln(is(typeof(v) == Vec3!S, S)); // false
> ```

The problem is that `typeof(v)` gets re-written very quickly by 
the compiler to `Matrix!(float, 3, 1)`, but the compiler can't 
tell that a `Matrix!(float, 3, 1)` is actually a `Vec3!S` for 
some generic `S` (it can't figure out what `S` should be). For a 
specific `S` it can. For instance `writeln(is(typeof(v) == 
Vec3!float));` prints true. So something like

`void foo(U)(U v) if (is(U == Vec3!float) || is(U == 
Vec3!double)) {}`

works, even though it is more awkward.

What we need is some way to inform the compiler of the reverse, 
i.e. that a Matrix!(float, 3, 1) is also a Vec3!float. I had 
sketched out an idea in the comments on issue 1807 [1], but it 
was rather half-baked and I'm not sure that's the right way to 
go. The underlying issue is that you need some kind of way to 
tell the compiler about the mapping from some concrete type to 
the template alias.

[1] https://issues.dlang.org/show_bug.cgi?id=1807


More information about the Digitalmars-d mailing list