应该Either
使用参数化函数吗?
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>))