www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Templates...

reply kinghajj <kinghajj_member pathlink.com> writes:
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?
Jul 17 2004
next sibling parent reply Cabal <cabalN05P4M myrealbox.com> writes:
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?

Jul 17 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
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/
Jul 17 2004
parent reply "Bent Rasmussen" <exo bent-rasmussen.info> writes:
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.
Jul 17 2004
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
 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?
Jul 17 2004
parent "Bent Rasmussen" <exo bent-rasmussen.info> writes:
Its a mechanism to partially unroll a loop, named after its inventor, Tom
Duff.

http://www.lysator.liu.se/c/duffs-device.html
Jul 17 2004
prev sibling next sibling parent Andrew Edwards <ridimz_at yahoo.dot.com> writes:
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
Jul 17 2004
prev sibling parent reply Ben Hinkle <bhinkle4 juno.com> writes:
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.
Jul 17 2004
parent "Blandger" <zeroman aport.ru> writes:
 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?
Jul 18 2004