The Next Big Language
Jonathan M Davis
jmdavisProg at gmx.com
Thu Oct 21 16:51:03 PDT 2010
On Thursday, October 21, 2010 12:45:32 Paulo Pinto wrote:
> Sorry but I still don't get it.
>
> Do you mean that the types that erased and the same code is generated?
>
> Then let me say that .Net generics get generated on the fly and JITed for
> each
> different type.
>
> Eiffel and Modula-3 generics also have specific generated code for each
> type.
>
> The major difference regarding C++ code is that the linkers are smarter and
> are
> able to remove duplicates of the generated code for the same set of type
> arguments.
Whether code gets consolidated really doesn't have anything to do with templates
- that's purely an optimization, and where exactly you draw the line betwene
generics and templates could be bit blurry, but they are two very different
things.
Generics allow for generic code - primarily container types. You're able to use
multiple types with the same code. Whether duplicate code is generated or not is
an implementation issue, but most languages that talk about having generics
don't seem to create full-on new sets of code for each type that you use - or if
they do, it's completely hidden from the programmer. Java's generics are
completely a compile-time artifact with the exact same code being generated - it
just gains you extra type-safety and saves you from explicit casts. I don't know
exactly how C# does it, but as I understand it, it uses a single definition
regardless of how many types you use with it but keeps the type information with
any variables declared of that type (it may JIT specific types on the fly as you
state - I have no idea). But regardless, they don't actually generate code in
your executable.
Templates are literally templates for creating code. When you instantiate a
template, it duplicates the entire definition for the type that it's being
instantiated for. vector<int> and vector<float> generate two completely separate
sets of source code. This is not hidden from the programmer. Templates can
generate different code depending on what they're instantiated with (typically
template specializations in C++, though D goes beyond that with conditional
compilation with static if and whatnot). So, even if you're limiting templates
to container types, you can end up with very different code for instantiations of
that template (a classic example in C++ being vector<bool>). Template
metaprogramming goes beyond that to do much crazier stuff with templates, but
even with out template metaprogramming, you still end up with whole new sets of
code for each template instantiation. The compiler may choose to optimize some
of that code and merge definitions where types are the same size, but that's an
optimization. Templates are literally a code-generation mechanism. They
therefore can be used for generic code or even could be considered generics, but
generics do not necessarily generate code.
Now, as I said, C++ and D are the only languages I know of which use templates.
That doesn't mean that other languages do not. Looking at the wikipedia page on
template metaprogramming, it lists other languages such as Eiffel and ML, so
presumably those languages have templates of some kind.
In any case, the key thing about templates is that they are a code-generation
mechanism. Generics are not (though templates can be used as generics).
- Jonathan M Davis
More information about the Digitalmars-d
mailing list