Search code examples
pythonclojurelisp

Type classes in dynamic languages


I have to admit that I have only basic knowledge of Python and currently learning Haskell.

I was wondering if the concept of type classes exist/makes sense in Python or in Clojure (or some other dynamically-strongly typed language) ?

In other words, if I have a function name f then depending on the run time parameter fed to f a different function implementation will be invoked (like the == function for types that belong to the Eq type class in Haskell). Does such a concept exist in dynamic languages such as Clojure/Python ?


Solution

  • You can get pretty close to this with multimethods or protocols in clojure, or with simple member functions (class methods) in python. There's one important feature missing in each of these that's present in haskell, though: return-type polymorphism.

    The compiler knows what type you "expect" a function to return, and can dispatch to a different implementation accordingly. This means that the same function, called on the same arguments, can do something entirely different, depending on what's done with its return value. For example:

    Prelude> read "[5,6,7]" :: [Int]
    [5,6,7]
    Prelude> read "[5,6,7]" :: [Double]
    [5.0,6.0,7.0]
    

    Likewise, you can even have polymorphic constants, which have a different value for each typeclass instance:

    Prelude Data.Word> (minBound, minBound, minBound) :: (Int, Bool, Word8)
    (-9223372036854775808,False,0)
    

    You can't really do this in a dynamic language because there's no type inference. You can fake it a little bit by passing around objects that represent "the type of result that I want", and use those as your dispatcher, but it's not really the same.