digitalmars.D - Template params: decl vs instantiation syntax
- Nick Sabalausky (14/14) Oct 06 2010 A trivial thing, but something I've been wondering about for awhile:
- Ellery Newcomer (3/17) Oct 06 2010 I have accidentally inserted the bang in the template decl so many times...
- Kagamin (4/7) Oct 06 2010 I thought, template instantiation in C++ is a little bit more complex li...
- Nick Sabalausky (12/20) Oct 07 2010 Been awhile since I used C++, so I guess I don't know, but C# does like ...
- Kagamin (2/24) Oct 07 2010 Aah, it's called Explicit Specialization. See factorial implementation a...
- Daniel Gibson (13/34) Oct 07 2010 because:
- Nick Sabalausky (16/48) Oct 07 2010 I think you misunderstood the question. I understand why there's a
- Daniel Gibson (3/62) Oct 07 2010 Ah ok, I read over "Why not use the exclamation for declaration too?"
A trivial thing, but something I've been wondering about for awhile: Function parameter syntax: Declare: foo(bar) Call: foo(bar) 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.
Oct 06 2010
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) 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.
Oct 06 2010
Nick Sabalausky Wrote: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?
Oct 06 2010
"Kagamin" <spam here.lot> wrote in message news:i8jps1$vhd$1 digitalmars.com...Nick Sabalausky Wrote: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>"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?Not sure what you mean here. Can you provide an example?
Oct 07 2010
Nick Sabalausky Wrote:Aah, it's called Explicit Specialization. See factorial implementation at http://digitalmars.com/d/2.0/template-comparison.htmlI thought, template instantiation in C++ is a little bit more complex like template<> class Foo<Bar>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?
Oct 07 2010
Nick Sabalausky schrieb:A trivial thing, but something I've been wondering about for awhile: Function parameter syntax: Declare: foo(bar) Call: foo(bar) 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
Oct 07 2010
"Daniel Gibson" <metalcaedes gmail.com> wrote in message news:i8kakj$230f$1 digitalmars.com...Nick Sabalausky schrieb: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 "!()".A trivial thing, but something I've been wondering about for awhile: Function parameter syntax: Declare: foo(bar) Call: foo(bar) 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 }
Oct 07 2010
Nick Sabalausky schrieb:"Daniel Gibson" <metalcaedes gmail.com> wrote in message news:i8kakj$230f$1 digitalmars.com...Ah ok, I read over "Why not use the exclamation for declaration too?" and thought you wanted to eliminate the ! for instantiation. Sorry.Nick Sabalausky schrieb: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 "!()".A trivial thing, but something I've been wondering about for awhile: Function parameter syntax: Declare: foo(bar) Call: foo(bar) 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 }
Oct 07 2010