Can I automatically implement classes?

后端 未结 2 1832
情歌与酒
情歌与酒 2021-01-17 21:42

In Scalaz every Monad instance is automatically an instance of Applicative.

implicit val listInstance = new Monad[List] {
  def po         


        
2条回答
  •  星月不相逢
    2021-01-17 22:05

    It isn't currently possible, though it would be if you changed the existing library to support this. Turning DefaultSignatures on would let you write

    class Applicative f where
        pure :: a -> f a
        (<*>) :: f (a -> b) -> f a -> f b
    
        default pure :: Monad f => a -> f a
        default (<*>) :: Monad f => f (a -> b) -> f a -> f b
        pure = return
        (<*>) = ap
    

    Then once you had implemented instance Monad M where {- ... -}, a simple instance Applicative M (with no where or method definitions) would inherit these default implementations. I'm not sure why this wasn't done.

提交回复
热议问题