Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 19, 2019 template parameter list syntax | ||||
---|---|---|---|---|
| ||||
Why is it that when template arguments are supplied at the point of use, the nice distinguishing syntax of x!(a,b,c) is obligatory, yet when template parameters are supplied at the point of definition the syntax is not distinguished in the same way with a bang ! |
January 19, 2019 Re: template parameter list syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carl Sturtivant | On Saturday, 19 January 2019 at 16:36:34 UTC, Carl Sturtivant wrote:
>
> Why is it that when template arguments are supplied at the point of use, the nice distinguishing syntax of x!(a,b,c) is obligatory, yet when template parameters are supplied at the point of definition the syntax is not distinguished in the same way with a bang !
At the point of use, ! is needed to ensure there's no ambiguity between instantiating a template and calling a function.
At the point of definition, there's no ambiguity, so the ! is not needed.
|
January 19, 2019 Re: template parameter list syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Saturday, 19 January 2019 at 16:40:42 UTC, Paul Backus wrote:
> On Saturday, 19 January 2019 at 16:36:34 UTC, Carl Sturtivant wrote:
>>
>> Why is it that when template arguments are supplied at the point of use, the nice distinguishing syntax of x!(a,b,c) is obligatory, yet when template parameters are supplied at the point of definition the syntax is not distinguished in the same way with a bang !
>
> At the point of use, ! is needed to ensure there's no ambiguity between instantiating a template and calling a function.
>
> At the point of definition, there's no ambiguity, so the ! is not needed.
Lots of things aren't logically needed. Syntax even: we could all program in an analog of FORTH using only trivial lexical structure.
However hopefully some programming languages are designed for natural intelligibility of some sort via the design of syntax.
In many languages the way parameters for functions are syntactically specified in a function definition mirrors the way arguments are supplied when the function is called, so that the syntax of the definition suggests the syntax of its use.
D doesn't do this with templates. Why?
|
January 19, 2019 Re: template parameter list syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carl Sturtivant | On Saturday, 19 January 2019 at 16:59:03 UTC, Carl Sturtivant wrote: > [...] > > In many languages the way parameters for functions are syntactically specified in a function definition mirrors the way arguments are supplied when the function is called, so that the syntax of the definition suggests the syntax of its use. > > D doesn't do this with templates. Why? Walter Bright explains some of the reasoning behind D's implementation of templates, including the syntax, in his article "Templates Revisited": https://dlang.org/articles/templates-revisited.html |
January 19, 2019 Re: template parameter list syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Saturday, 19 January 2019 at 17:59:48 UTC, Paul Backus wrote:
> On Saturday, 19 January 2019 at 16:59:03 UTC, Carl Sturtivant wrote:
>> [...]
>>
>> In many languages the way parameters for functions are syntactically specified in a function definition mirrors the way arguments are supplied when the function is called, so that the syntax of the definition suggests the syntax of its use.
>>
>> D doesn't do this with templates. Why?
>
> Walter Bright explains some of the reasoning behind D's implementation of templates, including the syntax, in his article "Templates Revisited":
>
> https://dlang.org/articles/templates-revisited.html
I don't see anything in this document that answers my design question. Do you?
|
January 19, 2019 Re: template parameter list syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carl Sturtivant | On Sat, Jan 19, 2019 at 08:06:36PM +0000, Carl Sturtivant via Digitalmars-d wrote: > On Saturday, 19 January 2019 at 17:59:48 UTC, Paul Backus wrote: > > On Saturday, 19 January 2019 at 16:59:03 UTC, Carl Sturtivant wrote: > > > [...] > > > In many languages the way parameters for functions are > > > syntactically specified in a function definition mirrors the way > > > arguments are supplied when the function is called, so that the > > > syntax of the definition suggests the syntax of its use. > > > > > > D doesn't do this with templates. Why? [...] The template function declaration syntax is actually a shorthand for the so-called eponymous template. When you write: auto myFunc(Args...)(Args args) { } it's actually a shorthand for: template myFunc(Args...) { auto myFunc(Args args) { } } For the shorthand to introduce ! would be incongruous because the template declaration syntax doesn't use !. T -- Fact is stranger than fiction. |
January 19, 2019 Re: template parameter list syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carl Sturtivant | On Saturday, 19 January 2019 at 20:06:36 UTC, Carl Sturtivant wrote:
> On Saturday, 19 January 2019 at 17:59:48 UTC, Paul Backus wrote:
>> Walter Bright explains some of the reasoning behind D's implementation of templates, including the syntax, in his article "Templates Revisited":
>>
>> https://dlang.org/articles/templates-revisited.html
>
> I don't see anything in this document that answers my design question. Do you?
Short of emailing Walter Bright and asking him directly, it's the closest thing to an answer I or anyone else can give you.
|
January 19, 2019 Re: template parameter list syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Saturday, 19 January 2019 at 20:52:06 UTC, H. S. Teoh wrote:
>
> The template function declaration syntax is actually a shorthand for the so-called eponymous template. When you write:
>
> auto myFunc(Args...)(Args args) { }
>
> it's actually a shorthand for:
>
> template myFunc(Args...) {
> auto myFunc(Args args) { }
> }
>
> For the shorthand to introduce ! would be incongruous because the template declaration syntax doesn't use !.
>
>
> T
I'm asking why template declaration syntax doesn't use ! so it's parallel to template use syntax which does use ! .
I was not talking about function templates as such.
|
January 19, 2019 Re: template parameter list syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carl Sturtivant | On Sat, Jan 19, 2019 at 10:52:25PM +0000, Carl Sturtivant via Digitalmars-d wrote: [...] > I'm asking why template declaration syntax doesn't use ! so it's parallel to template use syntax which does use ! . > > I was not talking about function templates as such. Ask Walter or Andrei. T -- It is widely believed that reinventing the wheel is a waste of time; but I disagree: without wheel reinventers, we would be still be stuck with wooden horse-cart wheels. |
January 20, 2019 Re: template parameter list syntax | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carl Sturtivant | On Saturday, 19 January 2019 at 22:52:25 UTC, Carl Sturtivant wrote: > On Saturday, 19 January 2019 at 20:52:06 UTC, H. S. Teoh wrote: >> >> The template function declaration syntax is actually a shorthand for the so-called eponymous template. When you write: >> >> auto myFunc(Args...)(Args args) { } >> >> it's actually a shorthand for: >> >> template myFunc(Args...) { >> auto myFunc(Args args) { } >> } >> >> For the shorthand to introduce ! would be incongruous because the template declaration syntax doesn't use !. >> >> >> T > > > I'm asking why template declaration syntax doesn't use ! so it's parallel to template use syntax which does use ! . > > I was not talking about function templates as such. 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 |
Copyright © 1999-2021 by the D Language Foundation