Skip to content

Commit

Permalink
Minor code style alterations and performance fixes.
Browse files Browse the repository at this point in the history
Specifically, avoid reinstantiating an immutable object to alter the type parameter *IF*
that type parameter has nothing to do with the contents of the object.
  • Loading branch information
jsuereth committed Sep 7, 2012
1 parent 4831ef5 commit ed04e04
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/library/scala/util/Try.scala
Expand Up @@ -157,11 +157,10 @@ object Try {
* method will ensure any non-fatal exception is caught and a
* `Failure` object is returned.
*/
def apply[T](r: => T): Try[T] = {
try { Success(r) } catch {
def apply[T](r: => T): Try[T] =
try Success(r) catch {
case NonFatal(e) => Failure(e)
}
}

}

Expand All @@ -175,18 +174,16 @@ final case class Failure[+T](val exception: Throwable) extends Try[T] {
case NonFatal(e) => Failure(e)
}
def get: T = throw exception
def flatMap[U](f: T => Try[U]): Try[U] = Failure[U](exception)
def flatten[U](implicit ev: T <:< Try[U]): Try[U] = Failure[U](exception)
def flatMap[U](f: T => Try[U]): Try[U] = this.asInstanceOf[Try[U]]
def flatten[U](implicit ev: T <:< Try[U]): Try[U] = this.asInstanceOf[Try[U]]
def foreach[U](f: T => U): Unit = ()
def map[U](f: T => U): Try[U] = Failure[U](exception)
def map[U](f: T => U): Try[U] = this.asInstanceOf[Try[U]]
def filter(p: T => Boolean): Try[T] = this
def recover[U >: T](rescueException: PartialFunction[Throwable, U]): Try[U] =
try {
if (rescueException isDefinedAt exception) {
Try(rescueException(exception))
} else {
this
}
} else this
} catch {
case NonFatal(e) => Failure(e)
}
Expand All @@ -197,7 +194,7 @@ final case class Failure[+T](val exception: Throwable) extends Try[T] {
final case class Success[+T](value: T) extends Try[T] {
def isFailure: Boolean = false
def isSuccess: Boolean = true
def recoverWith[U >: T](f: PartialFunction[Throwable, Try[U]]): Try[U] = Success(value)
def recoverWith[U >: T](f: PartialFunction[Throwable, Try[U]]): Try[U] = this
def get = value
def flatMap[U](f: T => Try[U]): Try[U] =
try f(value)
Expand Down

0 comments on commit ed04e04

Please sign in to comment.