Clojure
Subtyping on functional languages
This is almost a new post on the Function SOLID series. It should be about the Liskov Substitution Princible, but before we talk about it it’s important to understand the concept of subtyping.
Subtyping in Dynamic languages like Clojure or in languages that do not have hierarchical typing like Haskell seems strange. But subtyping is not only a concept about object-oriented programming languages – it’s about restrictions, and the concept of variance. I’m going to try to explain both in this post
To begin with, we can say that a supertype is more generic than a subtype, but that’s not all. In truth, is all about properties that you can prove about a specific data. For example, if we use Clojure as a starting language you could say that the coll?
is a property of maps and vectors, but vector?
is a property that only applies to vectors. In practice, this means that coll?
is a superclass of vector?
.
(coll? []) ; => true (coll? {}) ; => true (vector? []) ; => true (vector? {}) ; => false