Template and Type Literals

Nick Treleaven nick at geany.org
Wed May 22 11:03:41 UTC 2024


On Tuesday, 21 May 2024 at 17:38:41 UTC, Quirin Schroll wrote:
> A type literal is something like `struct { … }`, which you 
> could use as a template argument instead of a named struct.

Can't we use a type sequence instead? I would prefer built-in 
sequence syntax e.g. `__Seq(i: int, f: float)` rather than 
`struct {int i; float f;}`, as sequence literals can be used for 
any symbol sequence, not just types.

If the struct was a large type, it arguably would be better to 
make it a named type anyway, even if only used once. Then the 
code reads better.

> More interesting are template literals. There can’t be proper 
> template literals, though, but specialized ones.
>
> What I mean by “proper template literals” is that there can’t 
> be `template(T) =>` because what’s going to be after the `=>` 
> arrow? Templates “return” by eponymous member, but the template 
> has no name.

Now `=>` means `return`. We could just use `=`:

```d
alias(T) = const(T)
enum(T) = T.sizeof
```
I would require brackets for the parameters, because then the 
syntax is clearer and easy for the parser.

> For everything D has an abbreviated template syntax, there 
> should be a template lambda syntax using the same keyword, i.e. 
> `struct (T) { … }`, `class (T) { … }`, etc.

I'm a bit skeptical that those would be small enough to be 
written inline rather than being named.

> Normally, after `TemplateParameters`, there is an optional 
> `Constraint`, but that doesn’t really make sense here.

It might be useful with introspection.


More information about the dip.ideas mailing list