What exactly makes Option a monad in Scala?

后端 未结 4 1864
自闭症患者
自闭症患者 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:31

    Anything that (partially) implements, through duck-typing, the FilterMonadic trait is considered to be a monad in Scala. This is different than how monads are represented in Haskell, or the Monad typeclass in scalaz. However, in order to benefit of the for comprehension syntactic sugar in Scala, an object has to expose some of the methods defined in the FilterMonadic trait.

    Also, in Scala, the equivalent of the Haskell return function is the yield keyword used for producing values out of a for comprehension. The desugaring of yield is a call to the map method of the "monad".

提交回复
热议问题