3 ms·
What 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
by SamReidHughes 6y ago
What 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.