#Dynamic Polymorphism?
8 messages · Page 1 of 1 (latest)
When your question is answered use !solved or the button below to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
afaik there are basically two approaches: Inheritance-based and sum types
with the first approach, you specify an interface and don't care about the exact types that implement it.
With a sum type, you instead list the types that an object can have at runtime
So both approaches are good at different things; with inheritance it's easy to add new types, but relatively hard to add new functionality that doesn't just use what you've specified in the interface. And with a sum type it's the other way around; it's easy to write a new function but hard to change the sum type
Of course, how exactly these concepts are implemented changes depending on the language. Polymorphism through inheritance will usually use a vtable and require indirection.
In C++ this is implemented by storing a vptr in the object itself, but in Rust the vptr is instead stored alongside the pointer to the object, which makes it possible to add this runtime polymorphism to an existing type without needing to change it
I'd generally recommend looking at Rust; it's a neat language overall and how traits work for both runtime and compiletime polymorphism is a pretty cool idea (though not one that Rust came up with, afaik)
also, to be clear: When I said that there are two approaches, that doesn't mean you have to choose between them. It's very common to have both