4 ms·
C++ templates work more like LISP macros than they do like F#'s parametric polymorphism. This is partially by necessity: C++ is supposed to be backwards-compati
by Konohamaru 6y ago
C++ templates work more like LISP macros than they do like F#'s parametric polymorphism. This is partially by necessity: C++ is supposed to be backwards-compatible with C after all.... You can use templates to do parametric polymorphism (in fact all of the lambda cube), but they're really a macro system. Better than LISP's imo.
But do ALL implementations of parametric polymorphism boil down to "erasing and replacing" with generated code?
- SamReidHughes 6y agoWhat do you mean by erasing and replacing? What generated code do you mean? For instance, in Haskell you can define a type such as data Tree a = Single a | Bigger (Tree (Node23 a)) data Node23 a = Node2 a a | Node3 a a a And that will basically look like a linked list Bigger (Bigger (Bigger ...)) until it hits a fixed-depth 2,3-tree (for some depth) whose type might look something like Node23 (Node23 (Node23 (Node23 Int))). To actually recurse over the Node23 in code, you need type classes. It works just fine. The executable has finite size, and the type class instances you'd use would presumably get constructed on the fly, holding a reference to smaller types' instances. I'm not sure if that answers your question.
- Konohamaru 6y agoErasing and replacing = Erasing the parameters and replacing them with ad-hoc polymorphic functions generated by the compiler. Like what C++ does with templates. Basically do all languages with parametric polymorphism do that?
- SamReidHughes 6y agoAny language’s implementation can do that in places as an optimization. But Haskell is one example where it doesn’t generally do that, and the code above works. I never checked this question out for other languages, but Java, likewise, only supports generics on reference types and generates one bytecode per definition, so it should be similar to Haskell in that respect.