The Next Big Language

Paulo Pinto pjmlp at progtools.org
Thu Oct 21 23:00:05 PDT 2010


Sorry but all of them are just names for the same thing.

The template metaprogramming that is possibile in C++ is actually due to 
some loopholes
in the language definition. In the old days there were some articles in the 
C++ Report
when this technique was discovered.

D does provide a better way to do template metaprogramming, at least one 
that is more
user friendly.

Java generics are not that good, true. This is mainly to the fact that Sun 
decided to keep
backwards compatibility in the generated code. So actually Java generics are 
not much
more than sintactic sugar.

Eiffel, Modula-3, Ada, C#, F#, Scala all allow for type specialization.

Not sure if this is the same Wikipedia article that was already discussed 
before
http://en.wikipedia.org/wiki/Generic_programming

Thus the only thing I conced C++ and D templates have over the other 
languages is
the metaprogramming abilities. But there are other languages which 
compensate for
that by offering other languages features to handle similar funcionality.

An example, is what is possible to do with Lisp. And lets not forget that 
optimizing
Lisp compilers can beat Fortran code, which still has very good optimizing 
compilers.

--
Paulo

"Jonathan M Davis" <jmdavisProg at gmx.com> wrote in message 
news:mailman.763.1287705091.858.digitalmars-d at puremagic.com...
> 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