0

私が持っているコードはこれです:

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
}

そして intellij は暗黙を認識し、コンパイル時に map 関数を解決できません。ここで明らかに間違っていることはありますか?

4

1 に答える 1

0

カスタムの結合された暗黙のクラスを作成することで、これを解決できました。また、これは import scalaz._ がスコープ内にない限り機能します。

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)
}
于 2015-04-23T10:45:23.470 に答える