| Thread overview | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
October 07, 2010 Template params: decl vs instantiation syntax | ||||
|---|---|---|---|---|
| ||||
A trivial thing, but something I've been wondering about for awhile:
Function parameter syntax:
Declare: foo(bar)
Call: foo(bar)
Template parameter syntax in C++/C#/etc:
Declare: foo<bar>
Instantiate: foo<bar>
Template parameter syntax in D:
Declare: foo(bar)
Instantiate: foo!(bar)
Why the difference in syntax between declaring and instantiating? Why not use the exclamation for declaration too? Would that create a grammar ambiguity? Some other reason? No particular reason?
Obviously it's not a big issue, just curious.
| ||||
October 07, 2010 Re: Template params: decl vs instantiation syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | I have accidentally inserted the bang in the template decl so many times..
There's no grammatical reason to leave it out
On 10/06/2010 09:02 PM, Nick Sabalausky wrote:
> A trivial thing, but something I've been wondering about for awhile:
>
> Function parameter syntax:
> Declare: foo(bar)
> Call: foo(bar)
>
> Template parameter syntax in C++/C#/etc:
> Declare: foo<bar>
> Instantiate: foo<bar>
>
> Template parameter syntax in D:
> Declare: foo(bar)
> Instantiate: foo!(bar)
>
> Why the difference in syntax between declaring and instantiating? Why not
> use the exclamation for declaration too? Would that create a grammar
> ambiguity? Some other reason? No particular reason?
>
> Obviously it's not a big issue, just curious.
>
>
| |||
October 07, 2010 Re: Template params: decl vs instantiation syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | Nick Sabalausky Wrote:
> Template parameter syntax in C++/C#/etc:
> Declare: foo<bar>
> Instantiate: foo<bar>
I thought, template instantiation in C++ is a little bit more complex like template<> class Foo<Bar>
If you instantiate a template explicitly, how do you differentiate between declaration and instantiation?
| |||
October 07, 2010 Re: Template params: decl vs instantiation syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Kagamin | "Kagamin" <spam@here.lot> wrote in message news:i8jps1$vhd$1@digitalmars.com... > Nick Sabalausky Wrote: > >> Template parameter syntax in C++/C#/etc: >> Declare: foo<bar> >> Instantiate: foo<bar> > > I thought, template instantiation in C++ is a little bit more complex like template<> class Foo<Bar> > Been awhile since I used C++, so I guess I don't know, but C# does like I described above. Ie: class Foo<T> // Declare template {} class Bar { Foo<int> f; // Instantiate template } It's the same basic syntax either way: "xxx<yyy>" > If you instantiate a template explicitly, how do you differentiate between declaration and instantiation? Not sure what you mean here. Can you provide an example? | |||
October 07, 2010 Re: Template params: decl vs instantiation syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | Nick Sabalausky schrieb:
> A trivial thing, but something I've been wondering about for awhile:
>
> Function parameter syntax:
> Declare: foo(bar)
> Call: foo(bar)
>
> Template parameter syntax in C++/C#/etc:
> Declare: foo<bar>
> Instantiate: foo<bar>
>
> Template parameter syntax in D:
> Declare: foo(bar)
> Instantiate: foo!(bar)
>
> Why the difference in syntax between declaring and instantiating? Why not use the exclamation for declaration too? Would that create a grammar ambiguity? Some other reason? No particular reason?
>
> Obviously it's not a big issue, just curious.
>
>
because:
import std.stdio;
void fun(int X=3)(int a = 4){
writefln("X==%s a==%s", X, a);
}
void main() {
fun!(1)(2); // X==1, a==2
fun(2); // X==3, a==2
fun!(2); // X==2, a==4
}
Cheers,
- Daniel
| |||
October 07, 2010 Re: Template params: decl vs instantiation syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | Nick Sabalausky Wrote: > > I thought, template instantiation in C++ is a little bit more complex like template<> class Foo<Bar> > > > > Been awhile since I used C++, so I guess I don't know, but C# does like I described above. Ie: > > class Foo<T> // Declare template > {} > > class Bar > { > Foo<int> f; // Instantiate template > } > > It's the same basic syntax either way: "xxx<yyy>" > > > If you instantiate a template explicitly, how do you differentiate between declaration and instantiation? > > Not sure what you mean here. Can you provide an example? > Aah, it's called Explicit Specialization. See factorial implementation at http://digitalmars.com/d/2.0/template-comparison.html | |||
October 07, 2010 Re: Template params: decl vs instantiation syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Daniel Gibson | "Daniel Gibson" <metalcaedes@gmail.com> wrote in message news:i8kakj$230f$1@digitalmars.com... > Nick Sabalausky schrieb: >> A trivial thing, but something I've been wondering about for awhile: >> >> Function parameter syntax: >> Declare: foo(bar) >> Call: foo(bar) >> >> Template parameter syntax in C++/C#/etc: >> Declare: foo<bar> >> Instantiate: foo<bar> >> >> Template parameter syntax in D: >> Declare: foo(bar) >> Instantiate: foo!(bar) >> >> Why the difference in syntax between declaring and instantiating? Why not use the exclamation for declaration too? Would that create a grammar ambiguity? Some other reason? No particular reason? >> >> Obviously it's not a big issue, just curious. >> >> > > because: > > import std.stdio; > > void fun(int X=3)(int a = 4){ > writefln("X==%s a==%s", X, a); > } > > void main() { > fun!(1)(2); // X==1, a==2 > fun(2); // X==3, a==2 > fun!(2); // X==2, a==4 > } > > I think you misunderstood the question. I understand why there's a difference between function parameter syntax and template parameter syntax. What I don't understand is why there's a difference between the syntaxes for template instantiations and template declarations. Ie, why isn't D designed so that your 'fun' function above is like this?: // Note the "!": void fun!(int X=3)(int a = 4) {...} Or why class templates aren't like this?: class Foo!(T) {} For ordinary functions, you call *and* define using "()". In certain non-D langauges, templates/generics are instantiated *and* defined using "<>". In D, templates are instantiated with "!()", but they're defined with "()". I'm wondering why they're not instantiated *and* defined using "!()". | |||
October 07, 2010 Re: Template params: decl vs instantiation syntax | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | Nick Sabalausky schrieb:
> "Daniel Gibson" <metalcaedes@gmail.com> wrote in message news:i8kakj$230f$1@digitalmars.com...
>> Nick Sabalausky schrieb:
>>> A trivial thing, but something I've been wondering about for awhile:
>>>
>>> Function parameter syntax:
>>> Declare: foo(bar)
>>> Call: foo(bar)
>>>
>>> Template parameter syntax in C++/C#/etc:
>>> Declare: foo<bar>
>>> Instantiate: foo<bar>
>>>
>>> Template parameter syntax in D:
>>> Declare: foo(bar)
>>> Instantiate: foo!(bar)
>>>
>>> Why the difference in syntax between declaring and instantiating? Why not use the exclamation for declaration too? Would that create a grammar ambiguity? Some other reason? No particular reason?
>>>
>>> Obviously it's not a big issue, just curious.
>>>
>>>
>> because:
>>
>> import std.stdio;
>>
>> void fun(int X=3)(int a = 4){
>> writefln("X==%s a==%s", X, a);
>> }
>>
>> void main() {
>> fun!(1)(2); // X==1, a==2
>> fun(2); // X==3, a==2
>> fun!(2); // X==2, a==4
>> }
>>
>>
>
> I think you misunderstood the question. I understand why there's a difference between function parameter syntax and template parameter syntax. What I don't understand is why there's a difference between the syntaxes for template instantiations and template declarations. Ie, why isn't D designed so that your 'fun' function above is like this?:
>
> // Note the "!":
> void fun!(int X=3)(int a = 4)
> {...}
>
> Or why class templates aren't like this?:
>
> class Foo!(T) {}
>
> For ordinary functions, you call *and* define using "()". In certain non-D langauges, templates/generics are instantiated *and* defined using "<>". In D, templates are instantiated with "!()", but they're defined with "()". I'm wondering why they're not instantiated *and* defined using "!()".
>
Ah ok, I read over "Why not use the exclamation for declaration too?" and thought you wanted to eliminate the ! for instantiation. Sorry.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply