Does `is` expression with template alias need fixing.
Elfstone
elfstone at yeah.net
Fri Mar 17 05:19:44 UTC 2023
On Friday, 17 March 2023 at 00:40:02 UTC, Steven Schveighoffer
wrote:
> 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
Perhaps the compiler doesn't have to completely erase `Foo!float`
before it can solve the equation? I don't know. It definitely
looks easy to solve.
Again the current behaviour is of no use to anyone, and the
compiler lets the code pass without warning is not OK. Whenever
someone use a template alias (which s/he may not know is an
alias) in an `is` expression, or as a function parameter, it
should report an error, or warning, whatever.
SHOO's case is even more intolerable. Do you expect the user to
care about the hidden fact that `Regex` is an alias, and
`isInstanceOf` can't do its job because `Regex` is an alias, all
because `is` doesn't work with template aliases?
More information about the Digitalmars-d
mailing list