Either
應該帶參數化功能嗎?Scala:帶參數化功能類型
case class FunOrSeq[T1, T2](e: Either[Function1[T1, T2], Iterable[T1]])
def f: Int => Int = x => x
scala> FunOrSeq(Left(f))
<console>:11: error: type mismatch;
found : scala.util.Left[Int => Int,Nothing]
required: Either[T1 => Int,Iterable[T1]]
FunOrSeq(Left(f))
這是令人驚訝的,我 - 它的工作原理與明確的類型:
scala> case class FunOrSeq[T1, T2](e: Either[(Int => Int), Iterable[T1]])
defined class FunOrSeq
scala> FunOrSeq(Left(f))
res6: FunOrSeq[Nothing,Nothing] = FunOrSeq(Left(<function1>))
不是一個答案,但如果你在類型參數傳遞明確的'Left'以及你的第一個例子工程。 'FunOrSeq(Left [(Int => Int),Iterable [Int]](f))'看起來好像不能確定'[Int => Int,Nothing]'滿足'[T1 => Int,Iterable [T1]]' – Falmarri 2014-12-03 01:02:45