template parameter list syntax
Timon Gehr
timon.gehr at gmx.ch
Sun Jan 20 17:06:49 UTC 2019
On 20.01.19 17:34, Patrick Schluter wrote:
> On Sunday, 20 January 2019 at 16:22:09 UTC, Carl Sturtivant wrote:
>> On Sunday, 20 January 2019 at 12:35:58 UTC, Patrick Schluter wrote:
>>> On Saturday, 19 January 2019 at 22:52:25 UTC, Carl Sturtivant
>>>
>>> Afaik it's a parser problem. Walter wanted to avoid the catastrophic
>>> state that exist in C++ of an ambiguous grammar. He insited that the
>>> the grammar of the D language be as straightforward as can be.
>>> In the template declaration, both parenthesis pairs are required and
>>> the parser knows directly that first '(' == template parameter,
>>> second '(' rt parameter.
>>> At the instanciation site, it was preferred to have parameter
>>> inference, i.e. that the template parameter [1] can be omitted if it
>>> can be deduced from the runtime parameter. This has the consequence
>>> that the starting '(' becomes ambiguous, it can mean template
>>> parameter start or runtime parameter start. Using another operator
>>> allows to lift the ambiguity. Forcing to have parenthesis would also
>>> lift the ambiguity, but it would be at the cost of parameter
>>> inference as it would be impossible distinguish between infered
>>> parameter or no parameter. ! was chosen because it is light and its
>>> only use is in prefix context, in template instantiation it's in a
>>> infix position and thus completely unambiguous with the ! operator.
>>> [1]: https://tour.dlang.org/tour/en/basics/templates
>>
>> You've given a more detailed version of a response given earlier.
>> https://forum.dlang.org/post/xypridqjaqxosnxaqbjm@forum.dlang.org
>
> The point is that Walter Bright insists on context free grammar. This is
> a key point that allows for fast parsing. C++ is in the mess it is
> because it cannot be parsed (without heroic efforts). Every syntax
> choice has to abide by that rule. Template parameter inference was
> deemed MORE important than a slight inconsistency in the syntax of
> definition and use of the template. If that had not been the case, you
> would have to explicitely give always all type parameter in the template
> instanciation, every single time. This would make the code much more
> tedious, would violate DRY principle and would not even work properly as
> there are types in D that cannot even be written out (Voldemort types).
> D would be so much less expressive because of that.
> So, this slight inconsistency is a small price to pay for the benefits
> it provides.
It provides zero benefits. The grammar is still as context free as
before even if template declarations use consistent syntax, and the
following lookahead becomes unnecessary, making parsing of function
declarations simpler and more efficient:
https://github.com/dlang/dmd/blob/master/src/dmd/parse.d#L4123
It's just an annoying little design mistake. It happens.
I'm also not sure if you understood the OP. The suggestion is to have
this consistent syntax:
void foo!(T)(T arg){ ... }
void bar!T(T arg){ ... }
instead of this inconsistent syntax:
void foo(T)(T arg) { ... }
void bar(T)(T arg) { ... }
The syntax at the call site is not being criticized.
More information about the Digitalmars-d
mailing list