digitalmars.D - Templates...
- kinghajj (18/18) Jul 17 2004 I don't really understand Templates. The first example, which looks like...
- Cabal (14/38) Jul 17 2004 There are lots an lots of extremely cool things to be done with template...
- J C Calvarese (7/52) Jul 17 2004 In particular, I've tried to place some cool examples here:
- Bent Rasmussen (5/5) Jul 17 2004 The specification is probably not the best place for long real-world
- Jarrett Billingsley (3/5) Jul 17 2004 what the heck is a Duff's Device for? in the example it simply calls th...
- Bent Rasmussen (3/3) Jul 17 2004 Its a mechanism to partially unroll a loop, named after its inventor, To...
- Andrew Edwards (3/28) Jul 17 2004 Start here:
- Ben Hinkle (5/29) Jul 17 2004 I'd say 90% of use-cases for templates are for parametrized container
- Blandger (7/13) Jul 18 2004 I'm hardly too. So don't worry. I think there are a lots of people who d...
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
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
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.htmlIn particular, I've tried to place some cool examples here: http://www.dsource.org/tutorials/index.php?show_topic=TemplatesAlso, 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:-- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/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
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
I kind of like the example of a Duff's Devicewhat 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
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
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
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
kinghajj wrote: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 don't really understand Templates.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