如何使用类型推断和2类型参数来帮助Scalaz

我有一个叫做发电机的东西:

trait Generator[A, B] {
  def generate(in: Seq[A]): Seq[B]
}

我可以为这个生成器提供一个Bind实例:

object Generator {
  implicit def generatorBind[T]: Bind[({type l[B] = Generator[T, B]})#l] = new Bind[({type l[B] = Generator[T, B]})#l] {

    def map[A, B](generator: Generator[T, A])(f: A => B): Generator[T, B] = new Generator[T, B] {
      def generate(in: Seq[T]): Seq[B] = generator.generate(in).map(f)
    }

    def bind[A, B](generator: Generator[T, A])(f: A =>Generator[T, B]): Generator[T, B] = new Generator[T, B] {
      def generate(in: Seq[T]): Seq[B] = generator.generate(in).flatMap(v => f(v).generate(in))
    }
  }
}

不幸的是,如果我尝试将我的生成器用作应用实例,则类型推断完全丢失:

val g1 = new Generator[Int, Int] { def generate(seq: Seq[Int]) = seq.map(_ + 1) }
val g2 = new Generator[Int, Int] { def generate(seq: Seq[Int]) = seq.map(_ + 10) }

// doesn't compile
// can make it compile with ugly type annotations
val g3 = ^(g1, g2)(_ / _)

我现在唯一的解决方法是向Generator对象添加一个专门的方法:

def ^[T, A, B, C](g1: Generator[T, A], g2: Generator[T, B])(f: (A, B) => C) = 
  generatorBind[T].apply2(g1, g2)(f)

然后这个编译:

val g4 = Generator.^(g1, g2)(_ / _)

这个问题有解决方法吗?我想是因为使用State [S,A]作为Monad会产生同样的问题(但在Scalaz中似乎对State有特殊处理).

最佳答案 如果显式注释g1和g2类型,则可以使用
ApplicativeBuilder,或者更改为抽象类Generator

// java.lang.Object with Generator[Int, Int] !!!
val badInference = new Generator[Int, Int] { def generate(seq: Seq[Int]) = seq.map(_ + 1) }

val g1: Generator[Int, Int] = new Generator[Int, Int] { def generate(seq: Seq[Int]) = seq.map(_ + 1) }
val g2: Generator[Int, Int] = new Generator[Int, Int] { def generate(seq: Seq[Int]) = seq.map(_ + 10) }
val g3 = (g1 |@| g2)(_ / _)
点赞