functional programming - How to create an instance of typeclass Monad[F[_]] of a custom type constructor in Scala Cats -
how can create instance of custom type constructor in cats? know cats comes scala built-in type constructors , can import them e.g import cats.data._ import cats.implicits._ import cats.instances._ know how can create own implicit instance work monads operators sintaxs >>= own custom type constructor. suppose have mytype[a] monadic o @ least has flatmap , pure constructor, want things like:
monad[mytype].pure(x) >>= { _.foobar } i saw @ sources of instances package @ examples option seems lot do, don't know if there easy way.
regards.
just provide implicit instance of monad type, , implement required methods:
implicit object mymonad extends monad[mytype] { def pure[a](x: a): mytype[a] = ??? def flatmap[a, b](fa: mytype[a])(f: => mytype[b]): mytype[b] = ??? def tailrecm[a, b](a: a)(f: => mytype[either[a, b]]): mytype[b] = ??? } if confident monad stack-safe, can skip implementing tailrecm , extend stacksafemonad[mytype] instead.
Comments
Post a Comment