template auto value
Jonathan Marler
johnnymarler at gmail.com
Fri Mar 2 23:51:08 UTC 2018
I believe I found small hole in template parameter semantics.
I've summarized it here
(https://github.com/marler8997/dlangfeatures#template-auto-value-parameter). Wanted to get feedback before I look into creating a PR for it.
----------------------------------
COPY/PASTED from
https://github.com/marler8997/dlangfeatures#template-auto-value-parameter
----------------------------------
If you reference the D grammar for templates (See
https://dlang.org/spec/template.html), there are currently 5
categories of template parameters:
TemplateParameter:
TemplateTypeParameter
TemplateValueParameter
TemplateAliasParameter
TemplateSequenceParameter
TemplateThisParameter
However there is a hole in this list, namely, generic template
value parameters. The current TemplateValueParameter grammar node
must explicitly declare a "BasicType":
TemplateValueParameter:
BasicType Declarator
BasicType Declarator TemplateValueParameterSpecialization
BasicType Declarator TemplateValueParameterDefault
BasicType Declarator TemplateValueParameterSpecialization
TemplateValueParameterDefault
For example:
---
template foo(string value)
{
}
foo!"hello";
---
However, you can't create a template that accepts a value of any
type. This would a good use case for the auto keyword, i.e.
---
template foo(auto value)
{
}
foo!0;
foo!"hello";
---
This would be a simple change to the grammar, namely,
BasicTemplateType:
BasicType
auto
TemplateValueParameter:
BasicTemplateType Declarator
BasicTemplateType Declarator
TemplateValueParameterSpecialization
BasicTemplateType Declarator TemplateValueParameterDefault
BasicTemplateType Declarator
TemplateValueParameterSpecialization TemplateValueParameterDefault
More information about the Digitalmars-d
mailing list