Thread overview
Templates...
Jul 17, 2004
kinghajj
Jul 17, 2004
Cabal
Jul 17, 2004
J C Calvarese
Jul 18, 2004
Bent Rasmussen
Re: Templates... [semi-OT]
Jul 18, 2004
Bent Rasmussen
Jul 17, 2004
Andrew Edwards
Jul 17, 2004
Ben Hinkle
Jul 18, 2004
Blandger
July 17, 2004
I don't really understand Templates. The first example, which looks like this:

template Foo(T) {alias T* t;}
Foo!(int).t x; // x has type int*

This example looks kinda pointless. So I looked at another, which looked more useful:

template TCopy(T)
{
void copy(T from, out T to)
{
to = from;
}
}
int i;
TCopy!(int).copy(3,i);

This time, the example lives up to the name "template": it provides me with a 'template' of functions, so that I don't have to write a function for every type.

Are there any other uses for Templates besides this?


July 17, 2004
There are lots an lots of extremely cool things to be done with templates
when implemented properly. Unfortunately most examples tend to be on the
braindead side so that the basics can be easily comprehended - you're
expected to make an intuitive leap at some point (not that there's a
conspiracy of anything) when it all clicks into place and you go 'Wow! - I
can do this. And this! And That!'.
The more of the seemingly pointless examples you look at the more it'll seep
into your brain - take a look at dsource.org for some slightly more meaty
ones. And check out the D links page as well.

http://www.digitalmars.com/d/dlinks.html

Also, note that D currently seems to be suffering from a few(?) bugs which prevent the dustier corners being explored properly. Not that that will be an immediate problem if you're a newbie.

kinghajj wrote:

> I don't really understand Templates. The first example, which looks like this:
> 
> template Foo(T) {alias T* t;}
> Foo!(int).t x; // x has type int*
> 
> This example looks kinda pointless. So I looked at another, which looked more useful:
> 
> template TCopy(T)
> {
> void copy(T from, out T to)
> {
> to = from;
> }
> }
> int i;
> TCopy!(int).copy(3,i);
> 
> This time, the example lives up to the name "template": it provides me with a 'template' of functions, so that I don't have to write a function for every type.
> 
> Are there any other uses for Templates besides this?

July 17, 2004
kinghajj wrote:
> I don't really understand Templates. The first example, which looks like this:
> 
> template Foo(T) {alias T* t;}
> Foo!(int).t x; // x has type int*
> 
> This example looks kinda pointless. So I looked at another, which looked more
> useful:
> 
> template TCopy(T)
> {
> void copy(T from, out T to)
> {
> to = from;
> }
> }
> int i;
> TCopy!(int).copy(3,i);
> 
> This time, the example lives up to the name "template": it provides me with a
> 'template' of functions, so that I don't have to write a function for every
> type.
> 
> Are there any other uses for Templates besides this?
> 
> 

Start here:

http://digitalmars.com/d/template.html

July 17, 2004
Cabal wrote:
> There are lots an lots of extremely cool things to be done with templates
> when implemented properly. Unfortunately most examples tend to be on the
> braindead side so that the basics can be easily comprehended - you're
> expected to make an intuitive leap at some point (not that there's a
> conspiracy of anything) when it all clicks into place and you go 'Wow! - I
> can do this. And this! And That!'. 

I agree (though I think I'd replace "braindead" with "simplistic"). ;)

> The more of the seemingly pointless examples you look at the more it'll seep
> into your brain - take a look at dsource.org for some slightly more meaty
> ones. And check out the D links page as well.
> 
> http://www.digitalmars.com/d/dlinks.html

In particular, I've tried to place some cool examples here:
http://www.dsource.org/tutorials/index.php?show_topic=Templates

> 
> Also, note that D currently seems to be suffering from a few(?) bugs which
> prevent the dustier corners being explored properly. Not that that will be
> an immediate problem if you're a newbie.
> 
> kinghajj wrote:
> 
> 
>>I don't really understand Templates. The first example, which looks like
>>this:
>>
>>template Foo(T) {alias T* t;}
>>Foo!(int).t x; // x has type int*
>>
>>This example looks kinda pointless. So I looked at another, which looked
>>more useful:
>>
>>template TCopy(T)
>>{
>>void copy(T from, out T to)
>>{
>>to = from;
>>}
>>}
>>int i;
>>TCopy!(int).copy(3,i);
>>
>>This time, the example lives up to the name "template": it provides me
>>with a 'template' of functions, so that I don't have to write a function
>>for every type.
>>
>>Are there any other uses for Templates besides this?
> 
> 


-- 
Justin (a/k/a jcc7)
http://jcc_7.tripod.com/d/
July 17, 2004
kinghajj wrote:

> I don't really understand Templates. The first example, which looks like this:
> 
> template Foo(T) {alias T* t;}
> Foo!(int).t x; // x has type int*
> 
> This example looks kinda pointless. So I looked at another, which looked more useful:
> 
> template TCopy(T)
> {
> void copy(T from, out T to)
> {
> to = from;
> }
> }
> int i;
> TCopy!(int).copy(3,i);
> 
> This time, the example lives up to the name "template": it provides me with a 'template' of functions, so that I don't have to write a function for every type.
> 
> Are there any other uses for Templates besides this?

I'd say 90% of use-cases for templates are for parametrized container classes like LinkedList!(char[]) or SortedMap!(Foo,Bar) or whatever. If there aren't enough D examples about templates check out any template/STL tutorial for C++ and you'll get the idea.
July 18, 2004
The specification is probably not the best place for long real-world examples, but it may improve over time. I kind of like the example of a Duff's Device template mixin; in the mixin section. I think it'll take time before we see a lot of the useful things possible with the combination of nested functions, (D) templates and mixins realized.


July 18, 2004
> I kind of like the example of a
> Duff's Device

what the heck is a Duff's Device for?  in the example it simply calls the
foo() 10 times.  wouldn't a for() loop be just fine?  or is it an
optimization thing?


July 18, 2004
Its a mechanism to partially unroll a loop, named after its inventor, Tom Duff.

http://www.lysator.liu.se/c/duffs-device.html


July 18, 2004
> kinghajj wrote:
> > I don't really understand Templates.

I'm hardly too. So don't worry. I think there are a lots of people who don't understand many 'new' D stuff like mixins, templates etc. (me too). It's matter of time when we'll have good documentation, examples and less bugs using them.



"Ben Hinkle" <bhinkle4@juno.com> wrote in message news:cdc68n$1l4i$1@digitaldaemon.com...
> I'd say 90% of use-cases for templates are for parametrized container classes like LinkedList!(char[]) or SortedMap!(Foo,Bar) or whatever. If there aren't enough D examples about templates check out any template/STL tutorial for C++ and you'll get the idea.

Can you give a link, pls?