Template and Type Literals

Quirin Schroll qs.il.paperinik at gmail.com
Mon Jun 3 11:15:50 UTC 2024


On Friday, 31 May 2024 at 15:24:44 UTC, Nick Treleaven wrote:
> On Thursday, 30 May 2024 at 18:42:02 UTC, Atila Neves wrote:
>> Java inherited the same mistake, namely that we have to repeat 
>> the type's name to declare constructor(s) and the destructor. 
>> This makes refactoring harder, which is a Bad Thing(tm). In D 
>> we use `this` and Bob's your proverbial uncle.
>>
>> Unfortunately, we then went and did something nearly exactly 
>> the same with templates that's caused me hours of debugging 
>> when I decide to rename the thing and don't immediately catch 
>> it because of reflection or `__traits(compiles)`.
>>
>> Anyway, all to say that I've long wanted to write template 
>> lambdas and use `this` instead of a template's name.
>
> The trouble with using `this` for eponymous templates is that 
> it can't be used for eponymous template functions, because it 
> would conflict with constructor syntax. Instead I suggested 
> either the `template` keyword, or the `__self` identifier:
> https://issues.dlang.org/show_bug.cgi?id=7364#c4
> https://issues.dlang.org/show_bug.cgi?id=7364#c9
>
> The latter wouldn't need a grammar fix, and could also be 
> useful for lambda recursion.

If we had mixin identifiers, i.e. a clause that allowed 
[`Identifier`](https://dlang.org/spec/lex.html#Identifier) to be 
synthesized from a mixin, it could be as easy as adding a 
[special 
keyword](https://dlang.org/spec/expression.html#specialkeywords) 
`__TEMPLATE_NAME__` that resolves to a string naming the 
innermost template’s name. Then,
```d
alias mixin(__TEMPLATE_NAME__) = …;
```
does the magic. Template name would also be useful in things like 
`static assert` error messages.


More information about the dip.ideas mailing list