DIP 1023--Resolution of Template Alias Formal Parameters in Template Functions--Community Review Round 1

ag0aep6g anonymous at example.com
Wed Sep 11 14:14:31 UTC 2019

On 11.09.19 13:49, Stefanos Baziotis wrote:
> On Wednesday, 11 September 2019 at 05:32:22 UTC, ag0aep6g wrote:
>> A "template alias" is nothing but a (simple) template that resolves to 
>> an alias.
>> This:
>>     alias Foo(T) = ...;
>> is just short for this:
>>     template Foo(T) { alias Foo = T; }
>> They are exactly the same thing to the compiler.
> We should be more precise here. 2 things that are semantically the same
> might not be the same for the compiler (and the compiler's job is to handle
> them as the same).

Ok, sure. The difference is in syntax. It's the compiler's job to 
interpret the shorthand variant exactly like the longhand.

> A trivial example is this:
> ```
> alias my_int = int;
> my_int x;
> ...
> ```
> my_int and int are semantically exactly the same, but to the compiler 
> they're
> not. The compiler has to do work to handle them as the same.

And it might make sense to focus on that difference if you're writing a 
DIP that changes the meaning of `alias`. But if you're writing a DIP 
that changes the meaning of `int`, then we expect that is also applies 
to `my_int`.

So unless your DIP states that it changes the meaning of the alias 
template shorthand syntax, we expect that it also works with the longhand.

> I'm not saying that in your example they are or not, because I don't 
> know / remember. You might do though.
> But this is an important distinction. As far as I remember, they're
> not handled as the same and I wouldn't think that they're handled the same
> as well.

As far as I understand, you suspect a semantic difference. If that's so, 
you're simply mistaken.

> As another point, and this is the only important thing to a DIP (aka formal
> specification): 2 things might be semantically the same but not formally
> the same. The example above is in that category. The one is an alias 
> declaration
> and the other a built-in type. But the specification should say that they
> should be handled the same.

I'm not sure what "formally the same" would entail. Syntactically, the 
two variants are different (obviously). But that's it. Semantically, the 
shorthand is defined to mean whatever the longhand means [1].

> As far as I'm concerned, your example is a template declaration that 
> resolves
> to an alias declaration, which is different from an alias declaration.

Syntactically, `template Foo(T) { alias Foo = T; }` is called a 
"TemplateDeclaration". Semantically, it declares a template.

Syntactically, `alias Foo(T) = T;` is called an "AliasDeclaration". 
Semantically, it also declares a template. It doesn't declare an alias.

[1] https://dlang.org/spec/template.html#alias-template

More information about the Digitalmars-d mailing list