Should binary sharing be done using Mixin Template?
Jonathan M Davis
jmdavisProg at gmx.com
Sat May 21 18:17:33 PDT 2011
On 2011-05-21 09:13, so wrote:
> On Sat, 21 May 2011 15:49:16 +0300, bearophile <bearophileHUGS at lycos.com>
>
> wrote:
> > It's useful in the spots where the full performance of templates is not
> > needed (this happens in programs).
>
> What kind of applications are we talking about?
> I can't imagine a situation where you sacrifice the performance (that you
> gain with templates) for a trivial issue like bloat,
> when we finally get shared libraries, it will be even more trivial.
There are plenty of cases where programs don't care about effeciency much, so
any gain you get from templates over generics doesn't really matter. There are
also plenty of cases where it doesn't really matter that templates increase
the size of the executable in comparison to generics. It depends on what
you're doing. On honestly, I think that a lot of the arguments one way or the
other stem from perception rather than reality. Some people are brought to
believe that templates are bloated and that the way that generics do things is
inherently better because of that. Others are brought to believe that generics
are slow and that templates are better because of that. There is some truth to
both sides, but unless you're creating a programming language and choosing
whether you're going to implement templates or generics or if you're going to
use that as a factor in choosing which programming langage that you're going
to use, it doesn't really matter. Each language does it it's own way, and if
you're using a particular language, you're pretty much stuck with how it does
things. D happens to use templates, so if you're using D, you get templates
not generics.
Really, I think that the key thing that needs to be understood here is the key
nature of templates and how that differs from generics. If you're using Java,
you need to understand what generics are and how they work so that you can
best understand how to use them. Trying to make them act like templates
doesn't work. The same goes for D, except in reverse. You need to understand
templates an how they work so that you can best use them. Templates are
heavily used in Phobos, and anyone who doesn't understand what templates
really are is going to be at a disadvantage.
At their heart, templates are a tool for code generation. They allow you to
create functions and types which use different types without having to type
all of the different functions and types yourself, and they do it by
generating code. This has far-reaching consequences. One of the coolest is
eponymous templates (such as std.metastrings.Format). You couldn't do anything
like eponymous templates with generics.
The fact that templated stuff can be specialized on type and entirely change
what its implementation is based on its arguments is also incredibly powerful.
One instance of that that I find very cool is core.time.convert. It takes two
strings indicating the units of time that you want to convert to and from and
the value that you want to convert, and it is able to generate the exact
function which converts those specific units. With full optimizations on, it
probably becomes an inlined expression which is very short and exactly what is
needed for converting the value that you gave it to the units that it should
be in. You don't have to have a bunch of different functions for doing
conversions, and the function that you do use doesn't even need any if
statements. It's all done at compile time with static ifs, and the result
should be quite inlinable. You can't do anything like that with generics. You
can do it with templates because what they are is code generation, not a way
to inserts casts for you.
Ultimately, regardless of whether a programmer prefers generics or templates,
when they use a language that uses them, they need to understand what they are
and how they work so that they can best use them. Trying to pervert one into
the other is only going to give you trouble. One would hope that the compiler
writers would be able to optimize whichever the language uses in the best
manner possible, and I'm sure that more can be done in that area in D, but you
still need to understand the difference between generics and templates and why
the way that they are if you want to use them efficiently and appropriately.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list