On Monday, 15 October 2012 at 17:19:43 UTC, so wrote:
On Monday, 15 October 2012 at 16:37:08 UTC, Peter Alexander wrote:
...
- Simple(r) templates

I keep seeing things like this and probably i am failing to understand it. This is a vast understatement for D templates. Yes, easier to use, i agree. But C++ templates are stone age comparing to D and i don't see this mentioned enough where it matters most.

<snip>

I'm going to start a bit of a rant now.

D templates are certainly powerful, and are inarguably an improvement over C++ templates, but they are still based on C++ templates, and inherit a lot of their fundamental problems.

The main problem is the whole duck typing thing. Duck typing is the cause of all the horrible error messages in C++ templates, and D carried it over.

Consider this:

auto s = chain("a,b,c,", "d,e,f").splitter(",");

Seems like a reasonable thing to do, but you get an error:

std/algorithm.d(2020): Error: undefined identifier 'length'
... similar errors from inside std.algorithm ...

What am I supposed to make of that?

D attempts to mitigate this by adding template constraints, but this just makes the error messages shorter, not more useful. Here's another example, this time trying to instantiate a constrained template:

bool b = filter!(not!isPunctuation)("Hello, world!").endsWith("world");

Again, seems reasonable. I want to remove all the punctuation from a string and then test if it ends with the word "world". Again, I get an error:

Error: template std.algorithm.endsWith does not match any function template declaration std/algorithm.d(4375): Error: template std.algorithm.endsWith cannot deduce template function from argument types !()(FilterResult!(not,string),string)

This error message is no more useful than saying "you can't do that". Why doesn't any function match? Why can't it deduce the template parameters?

I don't believe there's any easy way to solve these problems with the current template design. You really need something like concepts or typeclasses to get quality errors. These aren't things that are easily tacked on.

So yeah, D's templates are better than C++'s, but they're still templates.

Reply via email to