Andrei Alexandrescu wrote:
Lutger wrote:
Andrei Alexandrescu wrote:
...
What the heck do you need generics for when you have real
templates? To me,
generics seem like just a lame excuse for templates.
I agree. Then, templates aren't easy to implement and they were
understandably already busy implementing the using statement.
Andrei
While I don't fully understand how generics work under the hood in
.NET, there are some benefits to how it is done. For example, you can
use runtime reflection on generic types. And the jit compiler
instantiates them at runtime. They may serve a different purpose than
templates:
"Anders Hejlsberg: To me the best way to understand the distinction
between C# generics and C++ templates is this: C# generics are really
just like classes, except they have a type parameter. C++ templates
are really just like macros, except they look like classes."
It seems that lack of structural typing is seen as a feature:
"When you think about it, constraints are a pattern matching
mechanism. You want to be able to say, "This type parameter must have
a constructor that takes two arguments, implement operator+, have this
static method, has these two instance methods, etc." The question is,
how complicated do you want this pattern matching mechanism to be?
There's a whole continuum from nothing to grand pattern matching. We
think it's too little to say nothing, and the grand pattern matching
becomes very complicated, so we're in- between."
From: http://www.artima.com/intv/genericsP.html
Oh, so Wal^H^H^Ha friend of mine I was talking to was right: there's
some missing of the point point going on. The code generation aspect of
templates is a blind spot of the size of Canada.
Andrei
I think you miss the point here.
Generics and code generation are two separate and orthogonal features
that where conflated together by C++.
while you can do powerful stuff with templates it smells of trying to
write Haskel code with the C pre-proceesor.
if you want to see a clean solution to this issue look at Nemerle.
essentially, their AST Macro system provides multi-level compilation.
c++ templates are a horrible hack designed to ween off C programmers
from using the pre-processor and the D templates provide mostly cosmetic
changes to this. they do not solve the bigger design issue.