多态函数中的Scala自动类型推断

多态函数中的Scala自动类型推断,scala,Scala,我怎样才能做到这一点?我手头的任务有点复杂,但归结起来: object Z { class B extends Function1[Int, Int] { def apply(i: Int): Int = i } def compose[T <: Function1[X, X], X](fcts: List[T]): Function1[X, X] = { fcts.reduce(_ andThen _) } def test() = { v

我怎样才能做到这一点?我手头的任务有点复杂,但归结起来:

object Z {
  class B extends Function1[Int, Int] {
    def apply(i: Int): Int = i
  }

  def compose[T <: Function1[X, X], X](fcts: List[T]): Function1[X, X] = {
    fcts.reduce(_ andThen _)
  }

  def test() = {
    val fcts = List.empty[B]

    // Unspecified type parameter X
    val composed: Function1[Int, Int] = compose[B](fcts)
  }
}
对象Z{
B类扩展函数1[Int,Int]{
def应用(i:Int):Int=i
}

def组合[TScala编译器在尝试推断多个级别的类型参数时做得不好。相反,删除
T确保这是一个解决方案会更简单。虽然不是我希望的那样。没有其他选项用于某些或其他Scala技巧?问题是,我的compose不完全接收列表[B](或函数1的列表。)比这要复杂一点。@MichelLemay您可能可以在不同的情况下做一些不同的事情,但在这种情况下,我看不到解决类型推断缺陷的方法。
def compose[A](fcts: List[Function1[A, A]]): Function1[A, A] = {
  fcts.reduce(_ andThen _)
}
val a: Int => Int = _ + 10
val b: Int => Int = _ * 2
val c: Int => Int = _ - 3

scala> val f = compose(List(a, b, c))
f: Int => Int = scala.Function1$$Lambda$1187/930987088@531ec2ca

scala> f(2)
res1: Int = 21