
Static (early/compile-time) binding of the function call to the function body is possible because the set of possible types and implementations is predefined and finite. Either the argument is coerced to a single type, so only one implementation is necessary, or the function is overloaded with different implementations for each type. Ad hoc polymorphism is when a function call results in a dispatch to one of one or more type-specific (monomorphic) functions, depending on the argument type. (Diagram after Cardelli and Wegner (1985), with embellishments).įirst, polymorphism is either ad hoc or universal. It’s time, then, for a broader look at polymorphism, but first we need a clearer understanding of what polymorphism is. While we may not be able to take advantage of the JVM’s ability to accelerate type-conditional dispatch, we are primarily interested in increasing the maintainability and flexibility of our code. Moreover, the basic idea behind protocols can be implemented in any dynamically typed languaged. These solutions are not as bad as they are made out to be, and they are also forms of polymorphism. In JavaScript, the more common and ready to hand solutions are the first or the third in the bullet points above: we change our interfaces, or we use monkey-patching. While the expression problem is helpful in provoking us to think about alternative forms of polymorphism, its treatment is too often overdetermined by the comparison with Java. In The Joy of Clojure, Fergus and Hauser paradoxically describe polymorphism as one of the “powerful aspects of OOP” which has been “improved” by Clojure (not an OOP language!). They permit this because, unlike in the classical object-oriented model, the functions that implement protocols are not stored on the datatypes, but on a namespace which may be reserved just for them (examples to follow). Protocols allow us to specify certain functions as an interface, implement these functions differently for different data types, having them dispatch on the type of the first argument, and extend them to new datatypes, all without any risk of namespace collision.
#Proteus for dummies code#
Wrap the new datatype inside an adapter object, which results in code proliferation.Go back and change our interfaces so they support the new datatype, which violates the O in SOLID (open for extension, closed for modification).

How do we make it consumable by the interfaces that we constructed to handle data with a different structure? Either: So imagine we are suddenly presented with a new datatype, perhaps from an external library. What is the “expression problem”? Unfortunately, the forms of data that an application will consume, and the operations it will need to perform on that data, are not likely to remain constant throughout its lifetime. The overriding methods will have different implementation details, but a consistent purpose, providing a single interface for manipulating different data types. In an OOP context, “polymorphism” almost always means just one thing: the overriding of inherited methods to facilitate calling the same method on different objects (so, for example, the chapter on OOP in Eloquent JavaScript). The so-called “expression problem” is a useful starting point for considering the advantages of different forms of polymorphism, since it exposes the limitations of the polymorphism peculiar to Java, which is often conflated with polymorphism in general. If you enjoy this post, please do clap 👏 , share, and follow 🐦 … It is truly appreciated! The expression problem In this post we will describe those kinds, and devote some thought to the forms of polymorphism that we can implement in JavaScript, with a focus on how and why we might wish to import the Clojurian concept of protocols. And since it is obvious that there are many kinds abstraction, it will be no surprise that there are many kinds of polymorphism.

Indeed, abstraction and polymorphism, considered abstractly, describe exactly the same thing.

Polymorphism - the ability to write a single function that handles many data-types - is a fundamental form of abstraction.

Program like Proteus - a beginner’s guide to polymorphism in JavaScript
