What exactly makes Option a monad in Scala?

后端 未结 4 1860
自闭症患者
自闭症患者 2020-12-13 04:09

I know what the monads are and how to use them. What I don\'t understand is what makes, let\'s say, Option a monad?

In Haskell a monad

4条回答
  •  眼角桃花
    2020-12-13 04:39

    Scala, per se, does not provide the notion of a monad. You can express a monad as a typeclass but Scala also doesn't provide the notion of a typeclass. But Cats does. So you can create a Monad in Scala with the necessary boiler plate, e.g. traits and implicits cleverly used, or you can use cats which provides a monad trait out of the box. As a comparison, Haskel provides monads as part of the language. Regarding your specific question, an Option can be represented as a monad because it has a flatMap method and a unit method (wrapping a value in a Some or a Future, for example).

提交回复
热议问题