In object oriented programming inheritance and virtual methods are the common scenario for creating extensible code. In more complex settings factory methods (or dependency frameworks) help extending the base code.
What are the common approaches in functional programming (eg F#) to create extensible code?
Fantastic question!
Here is an answer derived from this blog article:
The functional paradigm (meaning the use of higher-order functions) only provides a single form of extensibility: higher-order functions. These allow you to factor out “inner” functions. For example, code that often appears with the same first and last code blocks:
can be factored into a general higher order function that is reused from the specific cases:
Applying this aggressively leads to design patterns such as parser combinators and is a very powerful and lightweight technique for making code extensible. However, it does not make data types extensible.
But real functional programming languages almost always include funkier language features to help with extensibility:
Read Chris Okasaki’s excellent monograph Purely functional data structures for some great examples using higher-order modules in Standard ML and type classes in Haskell. Read Code reuse through polymorphic variants by Jacques Garrigue for a description of how that language feature can be used to attack the expression problem. However, these solutions are quite rare in the wild and, in particular, you can get a long way without them (e.g. in F#).
Historically, this diversity appeared because most functional programming languages were research projects and, consequently, they existed to add novel features. Therefore, we now have a wide variety of disparate forms of extensibility in today’s functional programming languages.
F# is a different beast because its design requirements were seamless interoperability with the rest of .NET (which imposes .NET-style OOP) and pragmatism. Consequently, F# keeps the ML core with parametric polymorphism and adds .NET’s object system. So you can benefit from the easy extensibility offered by generic higher-order functions and conventional OOP but not from any of the more esoteric features like higher-order modules, type classes and macros.
The only form of extensibility F# has pioneered is active patterns. These allow you to separate code that destructures via pattern matching from the concrete data representation. This is an important way to decouple code from data and, therefore, make it more reusable.