Reputation: 1518
I want to write a function which simply binds two monads together, without fixing in advance the exact type of the monads (Lists, State monads, etc.). It seems to me that this kind of genericity is the reason why typeclasses are so powerful, and that I should be able to do it with Scalaz. Here is what I have in mind:
def f[F[_], A](m1: F[A], m2: F[A]): F[(A,A)] =
m1 >>= { a: A => m2.map{ b: A => (a,b) }}
How do specify that F[_]
must implement the Monad typeclass so that I can use >>=
in my function? Writing F[_] <: Monad
doesn't seem to be the right approach since the types State, List, etc... which are monads don't extend the Monad trait.
Upvotes: 2
Views: 44
Reputation: 995
It sounds like context bounds are exactly what you're looking for. f[F[_] : Monad...
See What are Scala context and view bounds?
Upvotes: 4