Why the limited use of templates?

Oskar Linde oskar.lindeREM at OVEgmail.com
Fri Aug 25 07:12:31 PDT 2006


Reiner Pope wrote:
> I wonder why many of the functions in Phobos aren't templated. For 
> instance, the std.math functions could benefit from the user being able 
> to specify the type used, to avoid unnecessarily precise calculations. 
> Also, there's the same issue with std.string.
> 
> And in a more general view, templates are nice because of the duck 
> typing (or structural typing) that they allow. I was thinking about 
> creating a string substitute class in D, but doing so would require 
> redefining the std.string functions, whereas templated versions of those 
> functions would be satisfied if I just duplicated all the required methods.

I have written template function replacements for many of the functions 
in std.string, and I know Sean has done something along this way too.

For a rather old (and ugly) implementation, look at:
http://www.csc.kth.se/~ol/array.d

Doc (even worse):
http://www.csc.kth.se/~ol/array.html

I should wrap up my current working version any time now. The thing that 
is holding me back is mainly the DDoc bug with template functions.

> I'm not asking so that I can point out this code and say 'haha, that's 
> not being done properly,' but I'm actually wondering what the reasons 
> are. I think perhaps they are:
> 1. They were written at a time when D had an ugly template syntax.

Most of them were written even before D had any kind of templates.

> 2. Using templates requires the source code to compile instead of just 
> linking to the library files.

This is one of the disadvantages of using template functions. Others are 
currently non-working DDoc generation, and harder to understand error 
messages. I don't think any of them is the reason though.

> Any thoughts?
> 
> Are there reasons why phobos shouldn't be changed to support templated 
> versions of these functions?

Walter is the man behind the Phobos design. It would be interesting to 
hear what his plans and philosophies are regarding this.

/Oskar



More information about the Digitalmars-d-learn mailing list