Nobody understands templates?

Chris wendlec at tcd.ie
Mon Mar 3 08:40:08 PST 2014


On Sunday, 2 March 2014 at 18:59:23 UTC, Steve Teale wrote:
> On Sunday, 2 March 2014 at 15:23:03 UTC, H. S. Teoh wrote:
>> This is a pretty good primer to templates:
>>
>> 	https://semitwist.com/articles/article/view/template-primer-in-d
>>
>>
>>
> The trouble is with most of these tutorials that they offer 
> examples that are things you would probably never want to do. I 
> can already add an int to an int, or a double to a double, or 
> an int to a double.
>
> Perhaps the examples should pick on something like vector 
> operations, but then who would be doing those with int, or some 
> class? It would be doubles or pairs of, as in struct Coord.
>
> I believe readers would study documentation and examples much 
> more carefully if they were things they might realistically 
> want to do. And that won't be type conversion - std.conv 
> already does a pretty good job on that. So what?
>
> We could really do with a place where template savvy open 
> source contributors could publish interesting examples of 
> template use.
>
> Otherwise, Joe Soap, like me, can spend a great deal of time 
> and effort in:
>
> a) Determining when the use of a template might be advantageous,
> b) Hacking at test programs to determine what the documentation 
> means, and what works, and what doesn't.
> c) After that, deciding whether it would be just as effective 
> to use two or three separate methods.
>
> Steve
>
>
> Steve

I'm always willing to use templates, but maybe in fact the use 
cases are limited. I have a class for html elements (that 
implements DOM functionality), and a class for building trees 
with the tags. Of course, for html tags only string as a type 
makes sense.

class Element(T) {

}

or

class HTMLElement(T) if (is (T == string)) {

}

Tree(T) {

}

I implemented it as a template, because I thought it might be 
useful for other types as well, if I want to build a hierarchical 
tree of extracted data, be it integers, floating point numbers or 
whatever at a later point. However, I don't know a) if this will 
ever be the case and b) if I won't have to modify the template to 
adapt to new data types (which kinda defeats the purpose). I'm 
not against templates, I'm just not sure, if there are so many 
general or generalizable cases in programming for which templates 
are  _the_ solution.


More information about the Digitalmars-d-learn mailing list