summaryrefslogtreecommitdiff
path: root/src/library/scala/util/Try.scala
diff options
context:
space:
mode:
authoraleksandar <aleksandar@lampmac14.epfl.ch>2012-01-30 13:58:49 +0100
committeraleksandar <aleksandar@lampmac14.epfl.ch>2012-01-30 13:58:49 +0100
commit1d733556d04c4586b53e8bdd2d33211ebfb66baa (patch)
treed46b746f987aecc6024dda899fa131686849cdd8 /src/library/scala/util/Try.scala
parent7d206f3c7350a2d45a06bb49abf64530075d716f (diff)
downloadscala-1d733556d04c4586b53e8bdd2d33211ebfb66baa.tar.gz
scala-1d733556d04c4586b53e8bdd2d33211ebfb66baa.tar.bz2
scala-1d733556d04c4586b53e8bdd2d33211ebfb66baa.zip
Changed the comments a bit, removed on* methods on Try.
Diffstat (limited to 'src/library/scala/util/Try.scala')
-rw-r--r--src/library/scala/util/Try.scala87
1 files changed, 32 insertions, 55 deletions
diff --git a/src/library/scala/util/Try.scala b/src/library/scala/util/Try.scala
index 5dc5ede1cc..a810195b9f 100644
--- a/src/library/scala/util/Try.scala
+++ b/src/library/scala/util/Try.scala
@@ -6,84 +6,66 @@
** |/ **
\* */
+package scala.util
-package scala.util
/**
- * The Try type represents a computation that may either result in an exception,
- * or return a success value. It's analagous to the `Either` type.
- *
+ * The `Try` type represents a computation that may either result in an exception,
+ * or return a success value. It's analagous to the `Either` type.
*/
sealed abstract class Try[+T] {
/**
- * Returns true if the Try is a Failure, false otherwise.
+ * Returns true if the `Try` is a `Failure`, false otherwise.
*/
def isFailure: Boolean
/**
- * Returns true if the Try is a Success, false otherwise.
+ * Returns true if the `Try` is a `Success`, false otherwise.
*/
def isSuccess: Boolean
/**
- * Returns the value from this Success or the given argument if this is a Failure.
- */
- def getOrElse[U >: T](default: => U) = if (isSuccess) apply() else default
-
- /**
- * Returns the value from this Success or throws the exception if this is a Failure
+ * Returns the value from this `Success` or the given argument if this is a `Failure`.
*/
- def apply(): T
+ def getOrElse[U >: T](default: => U) = if (isSuccess) get else default
/**
- * Returns the value from this Success or throws the exception if this is a Failure.
- * Alias for apply()
+ * Returns the value from this `Success` or throws the exception if this is a `Failure`.
*/
- def get = apply()
+ def get
/**
* Applies the given function f if this is a Result.
*/
- def foreach[U](f: T => U) { onSuccess(f) }
+ def foreach[U](f: T => U): Unit
/**
- * Returns the given function applied to the value from this Success or returns this if this is a Failure.
- *
+ * Returns the given function applied to the value from this `Success` or returns this if this is a `Failure`.
*/
def flatMap[U](f: T => Try[U]): Try[U]
/**
- * Maps the given function to the value from this Success or returns this if this is a Failure
+ * Maps the given function to the value from this `Success` or returns this if this is a `Failure`.
*/
def map[U](f: T => U): Try[U]
/**
- * Converts this to a Failure if the predicate is not satisfied.
+ * Converts this to a `Failure` if the predicate is not satisfied.
*/
def filter(p: T => Boolean): Try[T]
/**
- * Calls the exceptionHandler with the exception if this is a Failure. This is like flatMap for the exception.
+ * Calls the exceptionHandler with the exception if this is a `Failure`. This is like `flatMap` for the exception.
*/
def rescue[U >: T](rescueException: PartialFunction[Throwable, Try[U]]): Try[U]
/**
- * Calls the exceptionHandler with the exception if this is a Failure. This is like map for the exception.
+ * Calls the exceptionHandler with the exception if this is a `Failure`. This is like map for the exception.
*/
def handle[U >: T](rescueException: PartialFunction[Throwable, U]): Try[U]
/**
- * Invoked only if the computation was successful.
- */
- def onSuccess[U](f: T => U): Try[T]
-
- /**
- * Invoked only if the computation failed.
- */
- def onFailure[U](rescueException: Throwable => U): Try[T]
-
- /**
* Invoked regardless of whether the computation completed
* successfully or unsuccessfully. Implemented in terms of
* `respond` so that subclasses control evaluation order. Returns a
@@ -93,9 +75,9 @@ sealed abstract class Try[+T] {
respond { _ => f }
/**
- * Returns None if this is a Failure or a Some containing the value if this is a Success
+ * Returns `None` if this is a `Failure` or a `Some` containing the value if this is a `Success`.
*/
- def toOption = if (isSuccess) Some(apply()) else None
+ def toOption = if (isSuccess) Some(get) else None
/**
* Invokes the given closure when the value is available. Returns
@@ -112,7 +94,7 @@ sealed abstract class Try[+T] {
/**
* Invokes the given transformation when the value is available,
* returning the transformed value. This method is like a combination
- * of flatMap and rescue. This method is typically used for more
+ * of `flatMap` and `rescue`. This method is typically used for more
* imperative control-flow than flatMap/rescue which often exploits
* the Null Object Pattern.
*
@@ -122,18 +104,19 @@ sealed abstract class Try[+T] {
f(this)
/**
- * Returns the given function applied to the value from this Success or returns this if this is a Failure.
- * Alias for flatMap
+ * Returns the given function applied to the value from this Success or returns this if this is a `Failure.
+ * Alias for `flatMap`.
*/
def andThen[U](f: T => Try[U]) = flatMap(f)
/**
- * Transforms a nested Try, i.e., a Try of type `Try[Try[T]]`,
- * into an un-nested Try, i.e., a Try of type `Try[T]`
+ * Transforms a nested `Try`, i.e., a `Try` of type `Try[Try[T]]`,
+ * into an un-nested `Try`, i.e., a `Try` of type `Try[T]`
*/
def flatten[U](implicit ev: T <:< Try[U]): Try[U]
}
+
final case class Failure[+T](e: Throwable) extends Try[T] {
def isFailure = true
def isSuccess = false
@@ -144,16 +127,12 @@ final case class Failure[+T](e: Throwable) extends Try[T] {
case e2 => Failure(e2)
}
}
- def apply(): T = throw e
+ def get: T = throw e
def flatMap[U](f: T => Try[U]): Try[U] = Failure[U](e)
def flatten[U](implicit ev: T <:< Try[U]): Try[U] = Failure[U](e)
+ def foreach[U](f: T => U) {}
def map[U](f: T => U): Try[U] = Failure[U](e)
def filter(p: T => Boolean): Try[T] = this
- def onFailure[U](rescueException: Throwable => U): Try[T] = {
- rescueException(e)
- this
- }
- def onSuccess[U](f: T => U): Try[T] = this
def handle[U >: T](rescueException: PartialFunction[Throwable, U]): Try[U] =
if (rescueException.isDefinedAt(e)) {
Try(rescueException(e))
@@ -162,35 +141,33 @@ final case class Failure[+T](e: Throwable) extends Try[T] {
}
}
+
final case class Success[+T](r: T) extends Try[T] {
def isFailure = false
def isSuccess = true
def rescue[U >: T](rescueException: PartialFunction[Throwable, Try[U]]): Try[U] = Success(r)
- def apply() = r
+ def get = r
def flatMap[U](f: T => Try[U]): Try[U] =
try f(r)
catch {
case e => Failure(e)
}
def flatten[U](implicit ev: T <:< Try[U]): Try[U] = r
+ def foreach[U](f: T => U) = f(r)
def map[U](f: T => U): Try[U] = Try[U](f(r))
def filter(p: T => Boolean): Try[T] =
if (p(apply())) this
else Failure(new NoSuchElementException("Predicate does not hold"))
- def onFailure[U](rescueException: Throwable => U): Try[T] = this
- def onSuccess[U](f: T => U): Try[T] = {
- f(r)
- this
- }
def handle[U >: T](rescueException: PartialFunction[Throwable, U]): Try[U] = this
}
-object Try {
+object Try {
+
def apply[T](r: => T): Try[T] = {
try { Success(r) } catch {
case e => Failure(e)
}
}
-
-} \ No newline at end of file
+
+}