March 01, 2014 Re: Nobody understands templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | On Friday, 28 February 2014 at 18:42:57 UTC, Steve Teale wrote:
> All the D aficionados seem to wet their pants over
> meta-programming, but I struggle to find a place to use it.
>
> IIRC, I used it in a couple of places when I was trying to write
> library stuff for MySQL, but in my current project, I use it only
> once. That's when I want to stuff something onto my undo stack.
>
> For that I have two template functions - push(T)(MybaseClass* p,
> T t, int ID), and pushC, which is just the same except that it
> checks the top of the stack to see if the ID there is the same as
> what it is wanting to push.
>
> This has served me very reliably, but I struggle to find other
> places in the whole application where I would benefit from
> templates.
>
> Is this typical - libraries use templates, applications don't, or
> am I just being unimaginative?
I use them for custom containers, configurations and the like,
which is the most obvious case -- when multiple types need to be
handled and I don't want to implement the same code for each
type. With my C and Java background (read, limited experience
with templates), it's been hard to recognize other uses. I was
ridiculously happy with myself recently when I realized I could
make my OpenGL vertex buffer code more flexible by using
templates and other compile time constructs to configure
different vertex formats, something that was rather non-obvious
to me before.
|
March 01, 2014 Re: Nobody understands templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Friday, 28 February 2014 at 19:06:26 UTC, Dicebot wrote: > On Friday, 28 February 2014 at 18:42:57 UTC, Steve Teale wrote: >> Is this typical - libraries use templates, applications don't, or >> am I just being unimaginative? >> >> Steve > > Also every time you catch yourself doing any sort of copy-paste, it is quite possible that it can be replaced by mixin / template mixin instead. Application that are built in more declarative way benefit more from such tool set than traditional imperative ones. In have a couple of case that always require me to find some other instance of it, and then copy/paste, But then I need to edit the pasted code anyway because in any particular case it needs to deal with different command info, so how can mixins help me? I have already dealt with the yada-yada cases by old-fashioned OOP. From the various answers to this question I have concluded that I should not worry about it. If there's a need for a template I think I am already recognizing it. Thanks!all() Steve |
March 01, 2014 Re: Nobody understands templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | You probably don't have a good understanding of templates if you have only used 2 in your entire codebase. Or you are talking about a very tiny codebase.
n Saturday, 1 March 2014 at 18:00:21 UTC, Steve Teale wrote:
> On Friday, 28 February 2014 at 19:06:26 UTC, Dicebot wrote:
>> On Friday, 28 February 2014 at 18:42:57 UTC, Steve Teale wrote:
>>> Is this typical - libraries use templates, applications don't, or
>>> am I just being unimaginative?
>>>
>>> Steve
>>
>
>> Also every time you catch yourself doing any sort of copy-paste, it is quite possible that it can be replaced by mixin / template mixin instead. Application that are built in more declarative way benefit more from such tool set than traditional imperative ones.
>
> In have a couple of case that always require me to find some other instance of it, and then copy/paste, But then I need to edit the pasted code anyway because in any particular case it needs to deal with different command info, so how can mixins help me? I have already dealt with the yada-yada cases by old-fashioned OOP.
>
> From the various answers to this question I have concluded that I should not worry about it. If there's a need for a template I think I am already recognizing it.
>
> Thanks!all()
> Steve
|
March 02, 2014 Re: Nobody understands templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to woh | On Saturday, 1 March 2014 at 22:16:54 UTC, woh wrote:
> You probably don't have a good understanding of templates if you have only used 2 in your entire codebase. Or you are talking about a very tiny codebase.
>
That's just what us template-blind people want to hear - confirmation that we are in fact stupid.
The project I'm talking about is about 30000 loc excluding blank lines and comments.
What I'd like to see is a tool, or a switch on the compiler that emits the code generated by templates. We - the template-blind - would have it sussed in a heartbeat then.
Steve
|
March 02, 2014 Re: Nobody understands templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | On Sunday, 2 March 2014 at 06:50:02 UTC, Steve Teale wrote:
> On Saturday, 1 March 2014 at 22:16:54 UTC, woh wrote:
>> You probably don't have a good understanding of templates if you have only used 2 in your entire codebase. Or you are talking about a very tiny codebase.
>>
> That's just what us template-blind people want to hear - confirmation that we are in fact stupid.
>
> The project I'm talking about is about 30000 loc excluding blank lines and comments.
>
> What I'd like to see is a tool, or a switch on the compiler that emits the code generated by templates. We - the template-blind - would have it sussed in a heartbeat then.
>
> Steve
There is nothing wrong about not using templates. Almost any compile-time design can be moved to run-time and expressed in more common OOP form. And using tool you have mastery of is usually more beneficial in practice than following the hype.
|
March 02, 2014 Re: Nobody understands templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Sunday, 2 March 2014 at 10:05:05 UTC, Dicebot wrote:
>
> There is nothing wrong about not using templates. Almost any compile-time design can be moved to run-time and expressed in more common OOP form. And using tool you have mastery of is usually more beneficial in practice than following the hype.
Yes DB, we can soldier on happily, but it would not do any harm to understand templates.
The documentation examples quickly make your eyes glaze over, looking at the code in Phobos is doubtless instructive, but you can wade through a lot of that without finding what you want. Also I discovered an interesting fact today. the word 'mixin' does not appear in the language reference Templates section of dlang.org.
It should be used in at least one example. I just discovered by trial and error that I could use 'mixin' in Templates (as opposed to Template Mixins), and when you know that it seems likely that you can accomplish lots of stuff you couldn't before.
While I'm here, has anyone discovered a way to fudge a constructor super(..) call in a mixin template that's included in a class constructor. Since the mixin template is evaluated in the scope of the constructor, it seems like it should be OK.
I'm sure I'll get there in time ;=)
Steve
|
March 02, 2014 Re: Nobody understands templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | On Sunday, 2 March 2014 at 11:47:39 UTC, Steve Teale wrote: > On Sunday, 2 March 2014 at 10:05:05 UTC, Dicebot wrote: > >> >> There is nothing wrong about not using templates. Almost any compile-time design can be moved to run-time and expressed in more common OOP form. And using tool you have mastery of is usually more beneficial in practice than following the hype. > > Yes DB, we can soldier on happily, but it would not do any harm to understand templates. > > The documentation examples quickly make your eyes glaze over, looking at the code in Phobos is doubtless instructive, but you can wade through a lot of that without finding what you want. Also I discovered an interesting fact today. the word 'mixin' does not appear in the language reference Templates section of dlang.org. > > It should be used in at least one example. I just discovered by trial and error that I could use 'mixin' in Templates (as opposed to Template Mixins), and when you know that it seems likely that you can accomplish lots of stuff you couldn't before. > > While I'm here, has anyone discovered a way to fudge a constructor super(..) call in a mixin template that's included in a class constructor. Since the mixin template is evaluated in the scope of the constructor, it seems like it should be OK. > > I'm sure I'll get there in time ;=) > > Steve This is quite a nice read on D templates: http://ddili.org/ders/d.en/templates.html |
March 02, 2014 Re: Nobody understands templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | On Sunday, 2 March 2014 at 11:47:39 UTC, Steve Teale wrote: > The documentation examples quickly make your eyes glaze over, looking at the code in Phobos is doubtless instructive, but you can wade through a lot of that without finding what you want. Try this: http://nomad.so/2013/07/templates-in-d-explained/ |
March 02, 2014 Re: Nobody understands templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | On Sun, Mar 02, 2014 at 11:47:38AM +0000, Steve Teale wrote: > On Sunday, 2 March 2014 at 10:05:05 UTC, Dicebot wrote: > > >There is nothing wrong about not using templates. Almost any compile-time design can be moved to run-time and expressed in more common OOP form. And using tool you have mastery of is usually more beneficial in practice than following the hype. > > Yes DB, we can soldier on happily, but it would not do any harm to understand templates. > > The documentation examples quickly make your eyes glaze over, looking at the code in Phobos is doubtless instructive, but you can wade through a lot of that without finding what you want. This is a pretty good primer to templates: https://semitwist.com/articles/article/view/template-primer-in-d T -- Дерево держится корнями, а человек - друзьями. |
March 02, 2014 Re: Nobody understands templates? | ||||
---|---|---|---|---|
| ||||
> https://semitwist.com/articles/article/view/template-primer-in-d http://nomad.so/2013/07/templates-in-d-explained/ http://ddili.org/ders/d.en/templates.html
That's a nice list. Is there a place on the wiki where these could be linked to?
|
Copyright © 1999-2021 by the D Language Foundation