Skip to content

Commit a203fbe

Browse files
committed
Tweak Try apply and doc
1 parent 566aa42 commit a203fbe

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

library/src/scala/util/Try.scala

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ import scala.runtime.Statics
1717
import scala.util.control.NonFatal
1818

1919
/**
20-
* The `Try` type represents a computation that may either result in an exception, or return a
21-
* successfully computed value. It's similar to, but semantically different from the [[scala.util.Either]] type.
20+
* The `Try` type represents a computation that may fail during evaluation by raising an exception.
21+
* It holds either a successfully computed value or the exception that was thrown.
22+
* This is similar to the [[scala.util.Either]] type, but with different semantics.
2223
*
23-
* Instances of `Try[T]`, are either an instance of [[scala.util.Success]][T] or [[scala.util.Failure]][T].
24+
* Instances of `Try[T]` are an instance of either [[scala.util.Success]][T] or [[scala.util.Failure]][T].
2425
*
25-
* For example, `Try` can be used to perform division on a user-defined input, without the need to do explicit
26-
* exception-handling in all of the places that an exception might occur.
26+
* For example, consider a computation that performs division on user-defined input.
27+
* `Try` can reduce or eliminate the need for explicit exception handling in all of the places
28+
* where an exception might be thrown.
2729
*
2830
* Example:
2931
* {{{
@@ -58,8 +60,6 @@ import scala.util.control.NonFatal
5860
* Serious system errors, on the other hand, will be thrown.
5961
*
6062
* ''Note:'': all Try combinators will catch exceptions and return failure unless otherwise specified in the documentation.
61-
*
62-
* `Try` comes to the Scala standard library after years of use as an integral part of Twitter's stack.
6363
*/
6464
sealed abstract class Try[+T] extends Product with Serializable {
6565

@@ -202,13 +202,22 @@ sealed abstract class Try[+T] extends Product with Serializable {
202202
}
203203

204204
object Try {
205-
/** Constructs a `Try` using the by-name parameter. This
206-
* method will ensure any non-fatal exception is caught and a
207-
* `Failure` object is returned.
205+
/** Constructs a `Try` using the by-name parameter as a result value.
206+
*
207+
* The evaluation of `r` is attempted once.
208+
*
209+
* Any non-fatal exception is caught and results in a `Failure`
210+
* that holds the exception.
211+
*
212+
* @param r the result value to compute
213+
* @return the result of evaluating the value, as a `Success` or `Failure`
208214
*/
209215
def apply[T](r: => T): Try[T] =
210-
try Success(r) catch {
211-
case NonFatal(e) => Failure(e)
216+
try {
217+
val r1 = r
218+
Success(r1)
219+
} catch {
220+
case NonFatal(e) => return Failure(e)
212221
}
213222
}
214223

@@ -247,7 +256,6 @@ final case class Failure[+T](exception: Throwable) extends Try[T] {
247256
override def fold[U](fa: Throwable => U, fb: T => U): U = fa(exception)
248257
}
249258

250-
251259
final case class Success[+T](value: T) extends Try[T] {
252260
override def isFailure: Boolean = false
253261
override def isSuccess: Boolean = true

0 commit comments

Comments
 (0)