patternMinor
What would "sum types with functions" look like in OOP?
Viewed 0 times
sumwhatwithlooklikewouldtypesfunctionsoop
Problem
It's fair to summarize classes in OOP as "product types with functions." However, couldn't there be something like "sum types with functions"? How would inheritance work with them?
I'm trying to scout if anybody has considered this question before in language design.
I'm trying to scout if anybody has considered this question before in language design.
Solution
The key to remember is that there's a sort of dualism between functional sum types, and OOP implementations of a superclass or interface:
-
Sum types require the variants for a type to be specified up front, and for every operation, requires that we specify the operation for each variant. Adding a new operation is easy, adding a variant requires refactoring all functions.
-
OOP requires that we specify the operations for a type up front, and each implementation/subclass specifies its own implementation for each operation. Adding a new variant is easy, but adding a new operation requires refactoring in each subclass.
In each case, we have all possible pairs of variants and operations, but we differ on how we group them: all the implementations for variants in an operation together, or all the operations for a variant together.
It's fair to summarize classes in OOP as "product types with functions."
At its most barebones version, maybe. But this ignores any subtyping structure. Many versions of OOP are much richer, with interfaces being analogous to existential types.
As for the question of "what if we just did sum-types with functions", this would be pretty boring. You would specify a bunch of variants for your type, and then a bunch of operations your type, which would have to specify an implementation for each variant.
This is basically just a normal OOP type with a single member variable that's a sum-type, and a bunch of functions operating on that variable.
-
Sum types require the variants for a type to be specified up front, and for every operation, requires that we specify the operation for each variant. Adding a new operation is easy, adding a variant requires refactoring all functions.
-
OOP requires that we specify the operations for a type up front, and each implementation/subclass specifies its own implementation for each operation. Adding a new variant is easy, but adding a new operation requires refactoring in each subclass.
In each case, we have all possible pairs of variants and operations, but we differ on how we group them: all the implementations for variants in an operation together, or all the operations for a variant together.
It's fair to summarize classes in OOP as "product types with functions."
At its most barebones version, maybe. But this ignores any subtyping structure. Many versions of OOP are much richer, with interfaces being analogous to existential types.
As for the question of "what if we just did sum-types with functions", this would be pretty boring. You would specify a bunch of variants for your type, and then a bunch of operations your type, which would have to specify an implementation for each variant.
This is basically just a normal OOP type with a single member variable that's a sum-type, and a bunch of functions operating on that variable.
Context
StackExchange Computer Science Q#79638, answer score: 7
Revisions (0)
No revisions yet.