-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add scala.Try
as alias of scala.util.Try
to scala
package.
#7425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,15 +20,15 @@ import scala.util.control.NonFatal | |
* The `Try` type represents a computation that may either result in an exception, or return a | ||
* successfully computed value. It's similar to, but semantically different from the [[scala.util.Either]] type. | ||
* | ||
* Instances of `Try[T]`, are either an instance of [[scala.util.Success]][T] or [[scala.util.Failure]][T]. | ||
* Instances of `Try[T]`, are either an instance of [[scala.util.Try.Success]][T] or [[scala.util.Try.Failure]][T]. | ||
* | ||
* For example, `Try` can be used to perform division on a user-defined input, without the need to do explicit | ||
* exception-handling in all of the places that an exception might occur. | ||
* | ||
* Example: | ||
* {{{ | ||
* import scala.io.StdIn | ||
* import scala.util.{Try, Success, Failure} | ||
* import scala.Try.{Success, Failure} | ||
* | ||
* def divide: Try[Int] = { | ||
* val dividend = Try(StdIn.readLine("Enter an Int that you'd like to divide:\n").toInt) | ||
|
@@ -61,6 +61,7 @@ import scala.util.control.NonFatal | |
* | ||
* `Try` comes to the Scala standard library after years of use as an integral part of Twitter's stack. | ||
* | ||
* @tparam T Type of the expected result value type. | ||
* @author based on Twitter's original implementation in com.twitter.util. | ||
* @since 2.10 | ||
*/ | ||
|
@@ -213,73 +214,82 @@ object Try { | |
try Success(r) catch { | ||
case NonFatal(e) => Failure(e) | ||
} | ||
} | ||
|
||
final case class Failure[+T](exception: Throwable) extends Try[T] { | ||
override def isFailure: Boolean = true | ||
override def isSuccess: Boolean = false | ||
override def get: T = throw exception | ||
override def getOrElse[U >: T](default: => U): U = default | ||
override def orElse[U >: T](default: => Try[U]): Try[U] = | ||
try default catch { case NonFatal(e) => Failure(e) } | ||
override def flatMap[U](f: T => Try[U]): Try[U] = this.asInstanceOf[Try[U]] | ||
override def flatten[U](implicit ev: T <:< Try[U]): Try[U] = this.asInstanceOf[Try[U]] | ||
override def foreach[U](f: T => U): Unit = () | ||
override def transform[U](s: T => Try[U], f: Throwable => Try[U]): Try[U] = | ||
try f(exception) catch { case NonFatal(e) => Failure(e) } | ||
override def map[U](f: T => U): Try[U] = this.asInstanceOf[Try[U]] | ||
override def collect[U](pf: PartialFunction[T, U]): Try[U] = this.asInstanceOf[Try[U]] | ||
override def filter(p: T => Boolean): Try[T] = this | ||
override def recover[U >: T](pf: PartialFunction[Throwable, U]): Try[U] = { | ||
val marker = Statics.pfMarker | ||
try { | ||
val v = pf.applyOrElse(exception, (x: Throwable) => marker) | ||
if (marker ne v.asInstanceOf[AnyRef]) Success(v.asInstanceOf[U]) else this | ||
} catch { case NonFatal(e) => Failure(e) } | ||
} | ||
override def recoverWith[U >: T](pf: PartialFunction[Throwable, Try[U]]): Try[U] = { | ||
val marker = Statics.pfMarker | ||
try { | ||
val v = pf.applyOrElse(exception, (x: Throwable) => marker) | ||
if (marker ne v.asInstanceOf[AnyRef]) v.asInstanceOf[Try[U]] else this | ||
} catch { case NonFatal(e) => Failure(e) } | ||
/** | ||
* Class `Failure[+T]` represent a computation of `T` is failed with a [[Throwable]]. | ||
* | ||
* @tparam T Type of the expected result value type. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked it with my local build. |
||
*/ | ||
final case class Failure[+T](exception: Throwable) extends Try[T] { | ||
override def isFailure: Boolean = true | ||
override def isSuccess: Boolean = false | ||
override def get: T = throw exception | ||
override def getOrElse[U >: T](default: => U): U = default | ||
override def orElse[U >: T](default: => Try[U]): Try[U] = | ||
try default catch { case NonFatal(e) => Failure(e) } | ||
override def flatMap[U](f: T => Try[U]): Try[U] = this.asInstanceOf[Try[U]] | ||
override def flatten[U](implicit ev: T <:< Try[U]): Try[U] = this.asInstanceOf[Try[U]] | ||
override def foreach[U](f: T => U): Unit = () | ||
override def transform[U](s: T => Try[U], f: Throwable => Try[U]): Try[U] = | ||
try f(exception) catch { case NonFatal(e) => Failure(e) } | ||
override def map[U](f: T => U): Try[U] = this.asInstanceOf[Try[U]] | ||
override def collect[U](pf: PartialFunction[T, U]): Try[U] = this.asInstanceOf[Try[U]] | ||
override def filter(p: T => Boolean): Try[T] = this | ||
override def recover[U >: T](pf: PartialFunction[Throwable, U]): Try[U] = { | ||
val marker = Statics.pfMarker | ||
try { | ||
val v = pf.applyOrElse(exception, (x: Throwable) => marker) | ||
if (marker ne v.asInstanceOf[AnyRef]) Success(v.asInstanceOf[U]) else this | ||
} catch { case NonFatal(e) => Failure(e) } | ||
} | ||
override def recoverWith[U >: T](pf: PartialFunction[Throwable, Try[U]]): Try[U] = { | ||
val marker = Statics.pfMarker | ||
try { | ||
val v = pf.applyOrElse(exception, (x: Throwable) => marker) | ||
if (marker ne v.asInstanceOf[AnyRef]) v.asInstanceOf[Try[U]] else this | ||
} catch { case NonFatal(e) => Failure(e) } | ||
} | ||
override def failed: Try[Throwable] = Success(exception) | ||
override def toOption: Option[T] = None | ||
override def toEither: Either[Throwable, T] = Left(exception) | ||
override def fold[U](fa: Throwable => U, fb: T => U): U = fa(exception) | ||
} | ||
override def failed: Try[Throwable] = Success(exception) | ||
override def toOption: Option[T] = None | ||
override def toEither: Either[Throwable, T] = Left(exception) | ||
override def fold[U](fa: Throwable => U, fb: T => U): U = fa(exception) | ||
} | ||
|
||
|
||
final case class Success[+T](value: T) extends Try[T] { | ||
override def isFailure: Boolean = false | ||
override def isSuccess: Boolean = true | ||
override def get = value | ||
override def getOrElse[U >: T](default: => U): U = get | ||
override def orElse[U >: T](default: => Try[U]): Try[U] = this | ||
override def flatMap[U](f: T => Try[U]): Try[U] = | ||
try f(value) catch { case NonFatal(e) => Failure(e) } | ||
override def flatten[U](implicit ev: T <:< Try[U]): Try[U] = value | ||
override def foreach[U](f: T => U): Unit = f(value) | ||
override def transform[U](s: T => Try[U], f: Throwable => Try[U]): Try[U] = this flatMap s | ||
override def map[U](f: T => U): Try[U] = Try[U](f(value)) | ||
override def collect[U](pf: PartialFunction[T, U]): Try[U] = { | ||
val marker = Statics.pfMarker | ||
try { | ||
val v = pf.applyOrElse(value, ((x: T) => marker).asInstanceOf[Function[T, U]]) | ||
if (marker ne v.asInstanceOf[AnyRef]) Success(v) | ||
else Failure(new NoSuchElementException("Predicate does not hold for " + value)) | ||
} catch { case NonFatal(e) => Failure(e) } | ||
/** | ||
* Class `Success[+T]` represent a computation of `T` is succeeded with a value of `T`. | ||
* | ||
* @tparam T Type of the expected result value type. | ||
*/ | ||
final case class Success[+T](value: T) extends Try[T] { | ||
override def isFailure: Boolean = false | ||
override def isSuccess: Boolean = true | ||
override def get: T = value | ||
override def getOrElse[U >: T](default: => U): U = get | ||
override def orElse[U >: T](default: => Try[U]): Try[U] = this | ||
override def flatMap[U](f: T => Try[U]): Try[U] = | ||
try f(value) catch { case NonFatal(e) => Failure(e) } | ||
override def flatten[U](implicit ev: T <:< Try[U]): Try[U] = value | ||
override def foreach[U](f: T => U): Unit = f(value) | ||
override def transform[U](s: T => Try[U], f: Throwable => Try[U]): Try[U] = this flatMap s | ||
override def map[U](f: T => U): Try[U] = Try[U](f(value)) | ||
override def collect[U](pf: PartialFunction[T, U]): Try[U] = { | ||
val marker = Statics.pfMarker | ||
try { | ||
val v = pf.applyOrElse(value, ((x: T) => marker).asInstanceOf[Function[T, U]]) | ||
if (marker ne v.asInstanceOf[AnyRef]) Success(v) | ||
else Failure(new NoSuchElementException("Predicate does not hold for " + value)) | ||
} catch { case NonFatal(e) => Failure(e) } | ||
} | ||
override def filter(p: T => Boolean): Try[T] = | ||
try { | ||
if (p(value)) this else Failure(new NoSuchElementException("Predicate does not hold for " + value)) | ||
} catch { case NonFatal(e) => Failure(e) } | ||
override def recover[U >: T](pf: PartialFunction[Throwable, U]): Try[U] = this | ||
override def recoverWith[U >: T](pf: PartialFunction[Throwable, Try[U]]): Try[U] = this | ||
override def failed: Try[Throwable] = Failure(new UnsupportedOperationException("Success.failed")) | ||
override def toOption: Option[T] = Some(value) | ||
override def toEither: Either[Throwable, T] = Right(value) | ||
override def fold[U](fa: Throwable => U, fb: T => U): U = | ||
try { fb(value) } catch { case NonFatal(e) => fa(e) } | ||
} | ||
override def filter(p: T => Boolean): Try[T] = | ||
try { | ||
if (p(value)) this else Failure(new NoSuchElementException("Predicate does not hold for " + value)) | ||
} catch { case NonFatal(e) => Failure(e) } | ||
override def recover[U >: T](pf: PartialFunction[Throwable, U]): Try[U] = this | ||
override def recoverWith[U >: T](pf: PartialFunction[Throwable, Try[U]]): Try[U] = this | ||
override def failed: Try[Throwable] = Failure(new UnsupportedOperationException("Success.failed")) | ||
override def toOption: Option[T] = Some(value) | ||
override def toEither: Either[Throwable, T] = Right(value) | ||
override def fold[U](fa: Throwable => U, fb: T => U): U = | ||
try { fb(value) } catch { case NonFatal(e) => fa(e) } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as requested.