Does `is` expression with template alias need fixing.

Elfstone elfstone at yeah.net
Fri Mar 17 05:54:00 UTC 2023


On Thursday, 16 March 2023 at 12:51:39 UTC, jmh530 wrote:
> 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.

Hm, maybe D should at least be able to resolve limited simple 
cases such as mine, and for cases it can't, report an error or 
warning.

Or just give up and report an error whenever it sees a template 
alias in `is` expression, because currently the result is just 
nonsense.

Just look at SHOO's code. What would a new user think when s/he 
finally finds out `isInstanceOf` doesn't work as expected, 
because `Regex` is an alias, and `isInstanceOf` internally uses 
`is` to resolve something it just can't? I know what I think: 
there should be an error message.

For every alias I create, I have to create a matching constraint 
for it, that's just sad.

```D
alias Vec2(S) = Matrix!(S, 2, 1);
enum isVec2(V) = is(V == Matrix!(S, 2, 1), S);
alias Vec3(S) = Matrix!(S, 3, 1);
enum isVec3(V) = is(V == Matrix!(S, 3, 1), S);
alias Vec4(S) = Matrix!(S, 4, 1);
enum isVec4(V) = is(V == Matrix!(S, 4, 1), S);
```


More information about the Digitalmars-d mailing list