scala.util.Failure 声明如下:
final case class Failure[+T](exception: Throwable) extends Try[T]`
Run Code Online (Sandbox Code Playgroud)
它需要一个T看起来完全不必要的类型参数,给出如何Failure轻松地声明为子类型Try[Nothing]:
final case class Failure(exception: Throwable) extends Try[Nothing]`
Run Code Online (Sandbox Code Playgroud)
以与None声明相同的方式:
object None extends Option[Nothing]
Run Code Online (Sandbox Code Playgroud)
实际上,额外的类型参数在其他地方成为痛点.这是Future.zip:
def zip[U](that: Future[U]): Future[(T, U)] = {
implicit val ec = internalExecutor
val p = Promise[(T, U)]()
onComplete {
case f: Failure[_] => p complete f.asInstanceOf[Failure[(T, U)]]
case Success(s) => that onComplete { c => p.complete(c map { s2 => (s, s2) }) }
}
p.future
} …Run Code Online (Sandbox Code Playgroud)