3

This is a follow-up to my previous question

I would like to generalize the implicit conversion toApplicative, which adds method <*> to any M[A=>B], where M is Applicative (i.e. there is a typeclass instance Applicative[M])

implicit def toApplicative[M[_], A, B](mf: M[A=>B])(implicit apm: Applicative[M]) = 
   new { def<*>(ma: M[A]) = apm.ap(ma)(mf) }

Unfortunately I have got an error:

   <console>:25: error: Parameter type in structural refinement 
            may not refer to an abstract type defined outside that refinement

How would you suggest implement such an implicit conversion ?

Community
  • 1
  • 1
Michael
  • 41,026
  • 70
  • 193
  • 341

1 Answers1

4

The types M and A are referenced from your def so they need to be part of the thing you're returning. I can't see any way to do this completely with the structural refinement, so I think you have to define a parametrized class; how about defining an implicit class rather than the implicit def?

implicit class PimpedApplicative[M[_]: Applicative, A, B](mf: M[A => B]) {
  def <*>(ma: M[A]) = Applicative[M].ap(ma)(mf)
}
lmm
  • 17,386
  • 3
  • 26
  • 37