Does `is` expression with template alias need fixing.

Elfstone elfstone at yeah.net
Thu Mar 16 01:57:34 UTC 2023


On Wednesday, 15 March 2023 at 13:07:53 UTC, jmh530 wrote:
> 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

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.


More information about the Digitalmars-d mailing list