March 18, 2012
A template is a parameterized namespace. That is, it is a namespace (a name through which other objects can be accessed) that may be passed parameters that can modify the nature of the stuff inside.

If a template is a compile-time function, then the equivalent of a function call - the association of a template description with
specific arguments - is called a template instantiation.

Templates have the following properties:

* they're unique; that is, a member of a template instantiation always refers the same as a member of the same instantiation in a different module

* they're compiletime; that is, it is impossible to instantiate a template while the program runs.

* In D, if a template contains only one member, and its name is the same as the template, then the member is assumed to *be* the template instantiation.

That's all!

In D, void foo(T)(T t) { } is just short for template foo(T) { void foo(T t) { } }.

So foo!(int) == "member foo of instantiation of template foo with parameter T = int".

There's a shortcut for this, called IFTI, "implicit function template instantiation". If you have a function template - that is,
a template containing only one function with the same name as the template - then calling the template as if it was a function will
simply instantiate it with the type of the arguments.

Example:

template bar(T...){ void bar(T t) { writefln(t); } }

bar(2, 3, 4); // is equivalent to
bar!(int, int, int)(2, 3, 4); // is equivalent to
bar!(int, int, int).bar(2, 3, 4);

March 18, 2012
On Sat, 17 Mar 2012 15:39:53 -0700
"H. S. Teoh" <hsteoh@quickfur.ath.cx> wrote:

> I don't think you can discard OOP entirely. It still has its place IMO. When you need runtime polymorphism, OOP is still the best tool for the job.

Hmm, if we want to write more FP-like type-safe code, I wonder how much we'd need runtime polymorphism at all?


Sincerely,
Gour


-- 
Never was there a time when I did not exist, nor you, nor all these kings; nor in the future shall any of us cease to be.

http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810


March 18, 2012
On Sun, 18 Mar 2012 17:01:00 +1100, FeepingCreature <default_357-line@yahoo.de> wrote:

> There's a shortcut for this, called IFTI, "implicit function template instantiation" ...
>

What would be useful is ...


 template bar(T...){ void bar(T t) { writefln(t); } }

 int a,b,c;
 bar!(a, b, c); // is equivalent to
 bar!(int, int, int).bar(a, b, c);

-- 
Derek Parnell
Melbourne, Australia
March 18, 2012
On 3/18/12, Derek <ddparnell@bigpond.com> wrote:
> What would be useful is ...
>   bar!(a, b, c); // is equivalent to
>   bar!(int, int, int).bar(a, b, c);

You mean like this?

template bar(T...)
{
    void bar() { writeln(T); }
}

void main()
{
    int a = 1, b = 2, c = 3;
    bar!(a, b, c);
}
March 18, 2012
"Andrej Mitrovic" <andrej.mitrovich@gmail.com> wrote in message news:mailman.851.1332059038.4860.digitalmars-d@puremagic.com...
> On 3/18/12, Derek <ddparnell@bigpond.com> wrote:
>> What would be useful is ...
>>   bar!(a, b, c); // is equivalent to
>>   bar!(int, int, int).bar(a, b, c);
>
> You mean like this?
>
> template bar(T...)
> {
>    void bar() { writeln(T); }
> }
>
> void main()
> {
>    int a = 1, b = 2, c = 3;
>    bar!(a, b, c);
> }

Shouldn't that be:

template bar(T...)
{
    void bar(T args) { writeln(args); }
}

void main()
{
    int a = 1, b = 2, c = 3;
    bar(a, b, c);
}

Or did I misunderstand the point?


March 18, 2012
Am 18.03.2012 07:41, schrieb Gour:
> On Sat, 17 Mar 2012 15:39:53 -0700
> "H. S. Teoh"<hsteoh@quickfur.ath.cx>  wrote:
>
>> I don't think you can discard OOP entirely. It still has its place
>> IMO. When you need runtime polymorphism, OOP is still the best tool
>> for the job.
>
> Hmm, if we want to write more FP-like type-safe code, I wonder how much
> we'd need runtime polymorphism at all?
>
>
> Sincerely,
> Gour
>
>

Enterprise software?
March 18, 2012
On Sun, 18 Mar 2012 19:16:02 +1100, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:

> On 3/18/12, Derek <ddparnell@bigpond.com> wrote:
>> What would be useful is ...
>>   bar!(a, b, c); // is equivalent to
>>   bar!(int, int, int).bar(a, b, c);
>
> You mean like this?
>
> template bar(T...)
> {
>     void bar() { writeln(T); }
> }
>
> void main()
> {
>     int a = 1, b = 2, c = 3;
>     bar!(a, b, c);
> }

Almost, but more like this ...

template add(X,Y,Z)
{
   X add(Y a, Z b)
   {
       return cast(X) (cast(X)a + cast(X)b);
   }
}

void main()
{
     double s;
     int   t;
     ulong u;

     s = 1.23;
     t = 123;
     u = 456;

    t = add!(u,s);

    writefln( "%s %s %s", s,t, u );
}



This currently errors with ...

  "Error: template instance add!(u,s) add!(u,s) does not match template declaration add(X,Y,Z)"

-- 
Derek Parnell
Melbourne, Australia
March 18, 2012
On 03/18/12 11:29, Derek wrote:
> On Sun, 18 Mar 2012 19:16:02 +1100, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
> 
>> On 3/18/12, Derek <ddparnell@bigpond.com> wrote:
>>> What would be useful is ...
>>>   bar!(a, b, c); // is equivalent to
>>>   bar!(int, int, int).bar(a, b, c);
>>
>> You mean like this?
>>
>> template bar(T...)
>> {
>>     void bar() { writeln(T); }
>> }
>>
>> void main()
>> {
>>     int a = 1, b = 2, c = 3;
>>     bar!(a, b, c);
>> }
> 
> Almost, but more like this ...
> 
> template add(X,Y,Z)
> {
>    X add(Y a, Z b)
>    {
>        return cast(X) (cast(X)a + cast(X)b);
>    }
> }
> 
> void main()
> {
>      double s;
>      int   t;
>      ulong u;
> 
>      s = 1.23;
>      t = 123;
>      u = 456;
> 
>     t = add!(u,s);
> 
>     writefln( "%s %s %s", s,t, u );
> }
> 
> 
> 
> This currently errors with ...
> 
>   "Error: template instance add!(u,s) add!(u,s) does not match template declaration add(X,Y,Z)"
> 
why would you do that

what do you want to _do_

it sounds like you're frantically trying to nail templates into a shape that they really really really aren't meant for

in any case what is wrong with auto add(T)(T t) { return t[0] + t[1]; }
March 18, 2012
On 03/18/12 11:36, FeepingCreature wrote:
> On 03/18/12 11:29, Derek wrote:
>> On Sun, 18 Mar 2012 19:16:02 +1100, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
>>
>>> On 3/18/12, Derek <ddparnell@bigpond.com> wrote:
>>>> What would be useful is ...
>>>>   bar!(a, b, c); // is equivalent to
>>>>   bar!(int, int, int).bar(a, b, c);
>>>
>>> You mean like this?
>>>
>>> template bar(T...)
>>> {
>>>     void bar() { writeln(T); }
>>> }
>>>
>>> void main()
>>> {
>>>     int a = 1, b = 2, c = 3;
>>>     bar!(a, b, c);
>>> }
>>
>> Almost, but more like this ...
>>
>> template add(X,Y,Z)
>> {
>>    X add(Y a, Z b)
>>    {
>>        return cast(X) (cast(X)a + cast(X)b);
>>    }
>> }
>>
>> void main()
>> {
>>      double s;
>>      int   t;
>>      ulong u;
>>
>>      s = 1.23;
>>      t = 123;
>>      u = 456;
>>
>>     t = add!(u,s);
>>
>>     writefln( "%s %s %s", s,t, u );
>> }
>>
>>
>>
>> This currently errors with ...
>>
>>   "Error: template instance add!(u,s) add!(u,s) does not match template declaration add(X,Y,Z)"
>>
> why would you do that
> 
> what do you want to _do_
> 
> it sounds like you're frantically trying to nail templates into a shape that they really really really aren't meant for
> 
> in any case what is wrong with auto add(T)(T t) { return t[0] + t[1]; }

oh

you may have misunderstood me

a template is a **compile time parameterized namespace**

its parameters are **types** and **constants**, not runtime values

"add" is a "namespace that is instantiated with the types float and"

OOOOOOOOOOOOOOOOOOOOH
I get what you want. :D

template add(T) {
  template add(U...) {
    auto add(U u) {
      T res;
      foreach (value; u) res += value;
      return res;
    }
  }
}

void main()
{
     double s;
     int   t;
     ulong u;

     s = 1.23;
     t = 123;
     u = 456;

     t = add!int(u, s);

    writefln( "%s %s %s", s, t, u );
}
March 18, 2012
On 03/18/12 11:39, FeepingCreature wrote:
> On 03/18/12 11:36, FeepingCreature wrote:
>> On 03/18/12 11:29, Derek wrote:
>>> On Sun, 18 Mar 2012 19:16:02 +1100, Andrej Mitrovic <andrej.mitrovich@gmail.com> wrote:
>>>
>>>> On 3/18/12, Derek <ddparnell@bigpond.com> wrote:
>>>>> What would be useful is ...
>>>>>   bar!(a, b, c); // is equivalent to
>>>>>   bar!(int, int, int).bar(a, b, c);
>>>>
>>>> You mean like this?
>>>>
>>>> template bar(T...)
>>>> {
>>>>     void bar() { writeln(T); }
>>>> }
>>>>
>>>> void main()
>>>> {
>>>>     int a = 1, b = 2, c = 3;
>>>>     bar!(a, b, c);
>>>> }
>>>
>>> Almost, but more like this ...
>>>
>>> template add(X,Y,Z)
>>> {
>>>    X add(Y a, Z b)
>>>    {
>>>        return cast(X) (cast(X)a + cast(X)b);
>>>    }
>>> }
>>>
>>> void main()
>>> {
>>>      double s;
>>>      int   t;
>>>      ulong u;
>>>
>>>      s = 1.23;
>>>      t = 123;
>>>      u = 456;
>>>
>>>     t = add!(u,s);
>>>
>>>     writefln( "%s %s %s", s,t, u );
>>> }
>>>
>>>
>>>
>>> This currently errors with ...
>>>
>>>   "Error: template instance add!(u,s) add!(u,s) does not match template declaration add(X,Y,Z)"
>>>
>> why would you do that
>>
>> what do you want to _do_
>>
>> it sounds like you're frantically trying to nail templates into a shape that they really really really aren't meant for
>>
>> in any case what is wrong with auto add(T)(T t) { return t[0] + t[1]; }
> 
> oh
> 
> you may have misunderstood me
> 
> a template is a **compile time parameterized namespace**
> 
> its parameters are **types** and **constants**, not runtime values
> 
> "add" is a "namespace that is instantiated with the types float and"
> 
> OOOOOOOOOOOOOOOOOOOOH
> I get what you want. :D
> 
> template add(T) {
>   template add(U...) {
>     auto add(U u) {
>       T res;
>       foreach (value; u) res += value;
>       return res;
>     }
>   }
> }
> 
> void main()
> {
>      double s;
>      int   t;
>      ulong u;
> 
>      s = 1.23;
>      t = 123;
>      u = 456;
> 
>      t = add!int(u, s);
> 
>     writefln( "%s %s %s", s, t, u );
> }

which of course doesn't work because you can't add a double to an int.

So .. maybe I don't get what you want.