My biggest problem with monads is that my brain can't grasp them, obviously! One of the issues I've got is that people teach them as part of a somewhat bigger context: they start showing how Option[T] works and then suddenly declare ''Option[T] is a monad''! Or worse, they talk about something completely unrelated in an attempt to give you some real world perspective onto them. It's not working for me and it might not be working for you; what I'll write here is to help me understand monads, and it probably won't work for you, but it might.
I'm going to start with an extremely simple case class
and build it up from there:
final case class Holder[+T](value: T)
It's really simple in that it holds a value of a type T
. One thing to realise is that case class
in Scala automatically gives you a companion object for Holder[T]
called Holder
which gives you an apply
method for creating instances. I'm going to make a similar object, which I'll abitrarily call HolderMonad
, that will have