I know the title is a mouthful so let me explain.
I want to implement custom monad FileM
by using the Cats Monad trait.
At the same time I want to define context bound for the type parameter so that
FileM
can only be used on types for which an instance of the Show
typeclass exists.
I've tried the following:
case class FileM[T : Show] private (value: T)
object FileM {
implicit val fileM: Monad[FileM] =
new Monad[FileM] {
override def pure[A](x: A): FileM[A] = {
new FileM[A](x) // could not find implicit value for evidence parameter of type cats.Show[A]
}
override def flatMap[A, B](fa: FileM[A])(f: A => FileM[B]): FileM[B] = ???
override def tailRecM[A, B](a: A)(f: A => FileM[Either[A, B]]): FileM[B] = ???
}
}
I've tried adding the context bound to the pure
method type parameter:
override def pure[A: Show](x: A): FileM[A] = {
new FileM[A](x)
}
But this changes the signature so it no longer overrides the pure
method from the Monad
trait.
How can I add context bound to type A
in this example?
I'm using scala 2.13.8
and cats-core:2.8.0