www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Template params: decl vs instantiation syntax

reply "Nick Sabalausky" <a a.a> writes:
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.
Oct 06 2010
next sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
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.

Oct 06 2010
prev sibling next sibling parent reply Kagamin <spam here.lot> writes:
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?
Oct 06 2010
parent reply "Nick Sabalausky" <a a.a> writes:
"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?
Oct 07 2010
parent Kagamin <spam here.lot> writes:
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?

Oct 07 2010
prev sibling parent reply Daniel Gibson <metalcaedes gmail.com> writes:
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
Oct 07 2010
parent reply "Nick Sabalausky" <a a.a> writes:
"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 "!()".
Oct 07 2010
parent Daniel Gibson <metalcaedes gmail.com> writes:
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.

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.
Oct 07 2010