Search code examples
clojurecategory-theorymonoids

Clojure cats append nil behaviour


I am using funcool/cats, append monoid with the following code :

(m/mappend (maybe/just [1 2 3])
             nil
             (maybe/just [4 5 6])
             (maybe/nothing)) ;;=> #<Just [1 2 3 4 5 6]>

What is the rationale for treating nil as maybe/nothing ?

Note : the version is [funcool/cats "1.2.1"]


Solution

  • From the commit log, it seems like it's

    just for (sic) avoid accidental null pointer exceptions

    This is also documented here: http://funcool.github.io/cats/latest/#nil

    Given the fact that nil is both a value and a type, we have extended the nil type to be equivalent to Maybe monad’s Nothing.