Should I stop being interested in D language if I don't like to see template instantiation in my code?

berni44 dlang at d-ecke.de
Wed Nov 13 16:33:32 UTC 2019


On Wednesday, 13 November 2019 at 14:23:40 UTC, mipri wrote:
> 3. it's very clear what's a compile-time vs. a runtime 
> parameter to the template.

At least, if one understands the difference. I remember that I 
was not aware of this, when I started learning D and was a lot 
confused by the use of templates in Phobos. And maybe for BoQsc 
it's the same. Therefore: Take the second example: This could 
also be written as

s.formattedRead("%s!%s:%s", a, b, c);

Now you've got no ! anymore. The disadvantage of this is, that 
parsing "%s!%s:%s" happens at runtime, meaning it is done 
everytime, the function is executed. If there are lots of such 
reads, the code can get considerably slower. The alternative is

s.formattedRead!("%s!%s:%s")(a, b, c);

I put the parenthesis around "%s!%s:%s" to make it more clear, 
that this is also some sort of function argument, like a, b and 
c. But this time, the compiler allready parses "%s!%s:%s" at 
compiletime, which means, when running the program it allready 
knows, that there are no errors in that string and maybe (I don't 
know the details) also, which parameter to put where and so on. 
That's faster. :-)

Believe me: It's worth to learn about templates!


More information about the Digitalmars-d-learn mailing list