0

The code I have is this:

class SourceService[Out[+_]](implicit monad:Monad[Out]) {
  def doSomething:Out[String] =
    monad.point("Result")
}

class SimplifiedPipe[Out[+_], In[+_]]
  (myService:SourceService[In])
  (implicit monad:Monad[Out], pipe: ~>[In, Out]) {

  implicit def ~>[I[+ _], O[+ _], T](value: I[T])(implicit nt: ~>[I, O]): O[T]
  = nt.apply(value)

  implicit class lift[T, O[+ _]](m: O[T])(implicit monad: Monad[O]) {
    def flatMap[S](f: T => O[S]): O[S] =
      monad.bind(m)(f)

    def map[S](f: T => S): O[S] =
      monad.map(m)(f)

    def foreach(f: T => Unit) =
      monad.map(m)(f)
  }

  def run: Out[String] =
    for {
      s <- myService.doSomething
    } yield s
}

And intellij recognises the implicits, on compile it cannot resolve the map function. Anything Im obviously doing wrong here?

J Pullar
  • 1,915
  • 2
  • 18
  • 30
  • At a glance, any reason you don't want to just apply `pipe` explicitly? Also I wouldn't be too surprised if the covariance was getting you into trouble. – Travis Brown Apr 09 '15 at 19:01
  • Definitely an option. Currently I have been piping implicitly and bind explicitly. Still, good to understand if it could work, or why not. Covariance across the two implicits could be causing the problem? – J Pullar Apr 09 '15 at 20:13
  • Made the higherkinded types invariant. Made no difference. – J Pullar Apr 10 '15 at 08:35

1 Answers1

0

I was able to resolve this by creating a custom combined implicit class. Also this works as long as import scalaz._ is NOT in scope.

implicit class PipeMonad[Out[+_], In[+_], A](in: In[A])(implicit monad: Monad[Out], pipe:In ~> Out) {
  def flatMap[T](f:A => Out[T]):Out[T] =
    monad.bind(pipe(in))(f)
  def map[T](f:A => T):Out[T] =
    monad.map(pipe(in))(f)
}
J Pullar
  • 1,915
  • 2
  • 18
  • 30