Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 17, 2004 Templates... | ||||
---|---|---|---|---|
| ||||
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 Re: Templates... | ||||
---|---|---|---|---|
| ||||
Posted in reply to kinghajj | 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 Re: Templates... | ||||
---|---|---|---|---|
| ||||
Posted in reply to kinghajj | 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 Re: Templates... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Cabal | 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 Re: Templates... | ||||
---|---|---|---|---|
| ||||
Posted in reply to kinghajj | 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 Re: Templates... | ||||
---|---|---|---|---|
| ||||
Posted in reply to J C Calvarese | 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 Re: Templates... [semi-OT] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bent Rasmussen | > 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 Re: Templates... [semi-OT] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 Re: Templates... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | > 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? |
Copyright © 1999-2021 by the D Language Foundation