diff options
author | Josh Suereth <joshua.suereth@gmail.com> | 2012-09-06 20:04:22 -0400 |
---|---|---|
committer | Josh Suereth <joshua.suereth@gmail.com> | 2012-09-07 08:17:52 -0400 |
commit | ed04e04a42010eccbe57fa6a6efa7a08334ca9ed (patch) | |
tree | 466c37c789308f8ccbb551f9ae02b76d8304ddeb /src | |
parent | 4831ef51a7b46b6ba4b8e120d5ff2ba66d8f7bac (diff) | |
download | scala-ed04e04a42010eccbe57fa6a6efa7a08334ca9ed.tar.gz scala-ed04e04a42010eccbe57fa6a6efa7a08334ca9ed.tar.bz2 scala-ed04e04a42010eccbe57fa6a6efa7a08334ca9ed.zip |
Minor code style alterations and performance fixes.
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/util/Try.scala | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/src/library/scala/util/Try.scala b/src/library/scala/util/Try.scala index 39500d7aaa..7afbfcdd66 100644 --- a/src/library/scala/util/Try.scala +++ b/src/library/scala/util/Try.scala @@ -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) } - } } @@ -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) } @@ -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) |