Does `is` expression with template alias need fixing.

Steven Schveighoffer schveiguy at gmail.com
Fri Mar 17 00:40:02 UTC 2023


On 3/16/23 12:10 AM, Elfstone wrote:
> On Wednesday, 15 March 2023 at 11:59:00 UTC, SHOO wrote:
>> On Wednesday, 15 March 2023 at 06:47:38 UTC, Elfstone wrote:
>>> ```D
>>> writeln(is(Vec3!float == Vec3!S, S)); // false
>>> ```
>>
>> I recently faced a similar problem.
>>
>> ```d
>> import std;
>> static assert(isInstanceOf!(Array, Array!char)); // true
>> static assert(isInstanceOf!(Regex, Regex!char)); // false
>> ```
> 
> The compiler should at least report it as an error: `is` cannot handle 
> template alias.

It can't see that.

The issue is that you are looking through a one-way window. In the 
general case, the compiler can't solve the puzzle, because it doesn't 
know the relationship between the template and the thing it gets:

```d
alias Foo(T) = int;

// the following are equivalent to the compiler
is(Foo!float == Foo!T, T);
is(int == Foo!T, T);
```

How is the compiler supposed to figure out what T you used? By the time 
it sees the type, it's coming in as `int`, the alias template itself is 
completely gone.

Now, I believe the compiler could make a special case for simple 
aliases, but the rules have to be defined, and the language designers 
need to agree to add it.

It is a frustrating limitation. One I've brought up myself almost 16 
years ago: https://issues.dlang.org/show_bug.cgi?id=1653

I hope some day someone can find a nice way to solve it, at least for IFTI.

-Steve


More information about the Digitalmars-d mailing list