Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does scala.util.Failure have a type parameter?

Tags:

types

scala

scala.util.Failure is declared like this:

final case class Failure[+T](exception: Throwable) extends Try[T]`

It takes a type parameter T which looks completely unnecessary, given how Failure could just as easily be declared as a sub-type of Try[Nothing]:

final case class Failure(exception: Throwable) extends Try[Nothing]`

in the same way that None is declared like:

object None extends Option[Nothing]

Indeed, the extra type parameter becames a pain point elsewhere. Here is 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
}

The line:

    case f: Failure[_] => p complete f.asInstanceOf[Failure[(T, U)]]

could be simplified to:

    case f: Failure => p complete f

If failure had been declared a sub-type of Try[Nothing].

I feel like I must be missing something here. The only reason I could come up with for the type parameter is to declare that an expression represents the failure to compute a particular type, and to make explicit it is a failure unlike just using Try[T], but I can't imagine a situation where this would really be needed.

like image 849
wingedsubmariner Avatar asked Apr 07 '14 05:04

wingedsubmariner


1 Answers

The T in Failure[+T] comes in handy when trying to recover from the failure: recover[U >: T](rescueException: PartialFunction[Throwable, U]): Try[U]

like image 155
My other car is a cadr Avatar answered Nov 14 '22 10:11

My other car is a cadr