by fanf2 · 6y ago · 0 comments
- To add a case to a type, the type needs to be abstract, which allows you to declare concrete subtypes. (Julia's subtyping is not OOPy, and it doesn't have FP-style algebraic types.) Then you can add methods to generic functions that are called when passed an argument of your new concrete type, when they need special cases.
- To add operations to an existing type, define a new generic function. You can often do so using just abstract supertypes, and let the compiler optimize to concrete types, but you can also define special-case methods for specific concrete types if you need to.
> Open functions (functions that can be extended with new pattern-matches), open data-types (data types that can be extended with new patterns), and MultiMethods ('open' specialized polymorphic functions with 'open' set of classes), and PredicateDispatching, are all viable approaches.
This is somewhat comparable to OCaml's polymorphic variants, which only solve the expression problem if you give up the completeness check of pattern matching.
[1] yes, I know it does a lot of run-time stuff with things called types, but that's something fundamentally different. Really.