by bcoates · 12y ago · 0 comments
Unless you want a truly simple C template system, in which case aren't the C11 type sensitive macros sufficient?
Hey, that sounds cool. Do you have a link? A quick search doesn't seem to work.
The vtables on the other hand I will steer clear from. Closures are way more useful than class based polymorphism. Combine it with parametric polymorphism (templates), and you won't need to subclass anything ever again —okay, I'm exaggerating a little.
On that topic, it is possible to do cool things with macros with GCC extensions. A simple example of generic macros was discussed about a week ago: https://news.ycombinator.com/item?id=7896280
Also, I'm not sure I get this "type erasure" business. In ML languages, there is no such erasure. I guess you need this erasure because the language doesn't support parametric polymorphism to begin with? I'm thinking about Java, which hacked generics after the fact, and maintained backward compatibility through type erasure.
In C++, there are few type erasures. Generally, when we want a function to receive a closure, we write it like this (minus dark corner I have missed):
template <typename T, typename E>
for_each(T functor, vector<E>);
No base class in sight there, just lots of duplicated object code. (Which is better is left as an exercise to the reader.)