Jump to page: 1 26  
Page
Thread overview
Nobody understands templates?
Feb 28, 2014
Steve Teale
Feb 28, 2014
Adam D. Ruppe
Feb 28, 2014
Dicebot
Mar 01, 2014
Steve Teale
Mar 01, 2014
woh
Mar 02, 2014
Steve Teale
Mar 02, 2014
Dicebot
Mar 02, 2014
Steve Teale
Mar 02, 2014
John Colvin
Mar 02, 2014
Gary Willoughby
Mar 02, 2014
H. S. Teoh
Mar 02, 2014
Steve Teale
Mar 03, 2014
Chris
Mar 03, 2014
Chris
Mar 05, 2014
sclytrack
Mar 05, 2014
develop32
Mar 05, 2014
H. S. Teoh
Mar 06, 2014
develop32
Mar 06, 2014
H. S. Teoh
Mar 02, 2014
Philippe Sigaud
Mar 03, 2014
Frustrated
Mar 03, 2014
Tobias Pankrath
Mar 03, 2014
Chris
Mar 03, 2014
Dicebot
Mar 03, 2014
Chris
Mar 05, 2014
Nick Sabalausky
Mar 05, 2014
Chris
Mar 03, 2014
Frustrated
Mar 04, 2014
Chris
Mar 04, 2014
H. S. Teoh
Mar 04, 2014
Chris
Mar 04, 2014
H. S. Teoh
Mar 05, 2014
Nick Sabalausky
Mar 04, 2014
Russel Winder
Mar 04, 2014
Chris
Mar 04, 2014
H. S. Teoh
Mar 04, 2014
Jesse Phillips
Mar 04, 2014
H. S. Teoh
Mar 02, 2014
Szymon Gatner
Mar 05, 2014
Nick Sabalausky
Mar 05, 2014
bearophile
Mar 05, 2014
Nick Sabalausky
Mar 05, 2014
bearophile
Mar 05, 2014
Nick Sabalausky
Feb 28, 2014
FreeSlave
Feb 28, 2014
Meta
Feb 28, 2014
H. S. Teoh
Feb 28, 2014
Frustrated
Mar 01, 2014
Jesse Phillips
Mar 01, 2014
H. S. Teoh
Mar 01, 2014
Rikki Cattermole
Mar 01, 2014
Mike Parker
Mar 04, 2014
Sean Kelly
February 28, 2014
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?

Steve
February 28, 2014
A lot of my code doesn't use very many new templates either... my web code does use a few magic templates which fills in a lot of boilerplate, but the app code - the business logic - uses almost no templates at all. My xml/html dom.d library also uses very few templates, it just doesn't fit as well as plain classes there.

I think it just depends on what you're doing... if it's generic or boilerplate, templates rock, but much of a program isn't really generic.
February 28, 2014
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

It is quite true as a generalization but in practice border line between application and a library is not that clear. You often have common modules used in various parts of an application. Kind of internal mini-libraries.

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.
February 28, 2014
Well, when you're starting to use many templates you may end up with separate library.

Templates provide compile-time correctness, concepts (in Boost sense), policy-base design (see "Modern C++ Design: Generic Programming and Design Patterns Applied" by Andrei Alexandrescu) and compile-time choice of the most optimized way to do things. Eventually templates are here just to ease programming of applications and reduce boilerplate. All these things that we expect from libraries and that library's author must take into account.

D is multiparadigm language so you should not worry if you don't use some of its features. You may ask same question about delegates, attributes or user defined exceptions. The answer is simple: it all depends on your style, preferences and needs, your vision how perfect code should look like.
February 28, 2014
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?
>
> Steve

It really depends on what you're writing. If you look at Phobos, which is meant to be as general as possible, I'd estimate that about 70% of it is templated, possibly more. I find that general utilities (such as your push function) are pretty much always template, while most program logic isn't. If you find yourself writing mainly program logic with very little need for utility functions, then it shouldn't be surprising that you are not using templates that much. The one exception is when doing stuff that requires a lot of boilerplate; mixins and template mixins are extremely useful for that.
February 28, 2014
On Fri, Feb 28, 2014 at 06:42:57PM +0000, 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?
[...]

As others have said, it depends on your needs and what you're trying to accomplish. :) If you find that your code contains some functions that are generically applicable, you can turn them into templates. Or leave them as normal functions until another piece of code needs to call it with different parameter types, then turn it into a template. A lot of my code starts out that way and turn into templates once it becomes clear that they are generically applicable.

It's also some degree of future-proofing: I work with numerical software, for example, and knowing that one day I might desire to substitute the built-in floating point types with, say, something that can represent quadratic rationals (numbers of the form a+b*√c) exactly, is an indication to me that functions that operate on these quantities should be templated on the number type.

But on the other hand, if something doesn't *need* to be a template, then don't use templates. :) It depends on what you're trying to accomplish. Use the right tools for the job. Not every problem is a nail to be hammered; sometimes using a screwdriver for a screw is just what you need.


T

-- 
There are two ways to write error-free programs; only the third one works.
February 28, 2014
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?
>

Yes.

Templates are awesome. I use them when ever I can. They allow you to simplify the tasks.

If you check out my thread about dependencies you can see a good example.

iGui             -->        iButton
 |
 |
 |> WindowsGui   -->        WindowsButton
 |
 |> LinuxGui     -->        LinuxButton


When programming to interfaces you lose the ability to use concrete types easily. E.g., WindowsGui can't use a WindowsButton because it has to implement iGui, which uses iButton... even though WindowsButton is an iButton and the only one we want to use with WindowsGui.

But as far as WindowsGui is concerned, there is no iButton but just a WindowsButton(Because it doesn't care about being general... and doesn't care about LinuxButton, RogueButton, or whatever).

In some sense, iButton was meant to be used with iGui and WindowsButton for WindowsGui... but we can't do this easily in D... well, unless you use a template to generate all the crap code you would normally have to write.

So a template(compile time construction) can help do things easier than you could otherwise... and if you do them right they are generic enough to be used in general things. (like anyone could use the template I created to help them with the same type of problem)

You are missing the point because the whole reason templates are generally used in libraries is because they are so powerful(you want to reuse them).


In the example I give above, the mixin template reduces code bloat and error proneness significantly for large classes and makes you feel like you are writing classes like you would normally write them when not using interface programming.

Basically until you start using templates a lot you won't know where to use them in the first place. You just gotta get used to them and then you'll start trying to use them everywhere.

(granted, I'm using mixin templates but templates nonetheless)
March 01, 2014
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

I believe it is typical. However it can also be definition, what is a library? What is an application? Certainly your application uses libraries, and it is very likely a library you need hasn't been written for your application, so you may end up writing libraries. If you write a templated function it is to be used in many places for many things, shouldn't it be in a library?

I believe that the majority of an application should be library. I can't claim to achieve this or that the libraries I create are not monolithic, but it is a view that I think helps to simplify the application.
March 01, 2014
In my experience in D, it comes down to one thing. Do I need to handle the type of something and act upon it?

This is shown heavily in dvorm and Cmsed's router/restful api/javascript generator.
In these cases I need to create code and have it string mixed in based upon a type.

However I'm not aware of many things needing this functionality.

March 01, 2014
On Sat, Mar 01, 2014 at 01:07:04AM +0000, Jesse Phillips 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
> 
> I believe it is typical. However it can also be definition, what is a library? What is an application? Certainly your application uses libraries, and it is very likely a library you need hasn't been written for your application, so you may end up writing libraries. If you write a templated function it is to be used in many places for many things, shouldn't it be in a library?
> 
> I believe that the majority of an application should be library. I can't claim to achieve this or that the libraries I create are not monolithic, but it is a view that I think helps to simplify the application.

+1. I find that my code tends to be cleaner and better designed when I write it as though it is going to be a library (even if it doesn't actually end up being a library). It's a good approach to improve the quality of the code I write.


T

-- 
If you compete with slaves, you become a slave. -- Norbert Wiener
« First   ‹ Prev
1 2 3 4 5 6