Template params: decl vs instantiation syntax

Nick Sabalausky a at a.a
Thu Oct 7 05:39:52 PDT 2010


"Daniel Gibson" <metalcaedes at gmail.com> wrote in message 
news:i8kakj$230f$1 at 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 "!()".




More information about the Digitalmars-d mailing list