r/functionalprogramming • u/mister_drgn • Dec 31 '24
Question Languages that support downcasting at runtime
There seems to be a distinction between languages that allow you to downcast at runtime and those that don't. (Relatively) recent languages with some functional support like Scala, Swift, or even Go allow this. You can create a heterogeneous collection of elements that support some some interface or protocol, and then you can iterate over this collection and attempt to downcast each item back to its original concrete type.
This concept seems to be less well supported in classic (compiled) functional languages. In Haskell, you can create a heterogeneous collection using an existential type, but afaik there's no way to downcast from the existential type back to each value's original, concrete type. In Ocaml, you can make a heterogeneous collection with first-class modules, but again there's no way to downcast back to the original modules (I think something similar holds for objects in ocaml, but no one talks about objects in ocaml). There might be _some_ way to downcast in Haskell or Ocaml, but it isn't convenient or encouraged.
Is there a good reason some languages support downcasting and others do not? Presumably the languages that support it store type information with values at runtime, but I get the impression there's a philosophical difference, and not just an implementation difference. I know downcasting is sometimes considered slow and (perhaps) inelegant, but I've written experimental Swift code that downcasts all over the place, and I don't find an perceptible performance cost.
Thanks.
EDIT: This isn't necessarily a question about whether languages _should_ support downcasting. I recognize that in most languages you can achieve a heterogeneous collection using an enum type. Enum types have the disadvantage that they aren't easily extensible--if you want to add new types to your heterogeneous collection, you have to change the original enum definition, rather than making a change in a new file.
View all comments
Show parent comment
2
u/mister_drgn Jan 10 '25
In Swift, this does not depend on subtyping. You can do it with protocols, which are like haskell type classes, but weaker (no higher kinded types). The closest match I know of in Haskell is that you can make an existential type based on some type class. Maybe you can downcast from an existential type back to a concrete type using dynamic types? But in Swift, you can freely cast from one protocol to another, if the underlying concrete type happens to conform to both protocols.
This was one reason I ended up using Swift to reimplement a modeling framework that was originally written in Clojure. The ability to cast freely makes it easier to write code that (in certain respects) maintains the flexibility of dynamically typed code, while gaining the benefits of a statically typed language (type safety, null safety).
EDIT: Maybe the protocol to concrete type relationship is an example of what you mean by subtyping—I’m not sure. But it isn’t a simple hierarchy because the relationships between types and protocols are many-to-many.