Skip to content

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ abstract class DefaultMacroCompiler extends Resolvers
*/
def resolveMacroImpl: Tree = {
def tryCompile(compiler: MacroImplRefCompiler): scala.util.Try[Tree] = {
try { compiler.validateMacroImplRef(); scala.util.Success(compiler.macroImplRef) }
catch { case ex: MacroImplResolutionException => scala.util.Failure(ex) }
try { compiler.validateMacroImplRef(); scala.Try.Success(compiler.macroImplRef) }
catch { case ex: MacroImplResolutionException => scala.Try.Failure(ex) }
}
val vanillaImplRef = MacroImplRefCompiler(macroDdef.rhs.duplicate, isImplBundle = false)
val (maybeBundleRef, methName, targs) = macroDdef.rhs.duplicate match {
Expand Down
9 changes: 2 additions & 7 deletions src/compiler/scala/tools/nsc/plugins/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@
package scala.tools.nsc
package plugins

import scala.tools.nsc.io.Jar
import scala.reflect.internal.util.ScalaClassLoader
import scala.reflect.io.{Directory, File, Path}
import java.io.InputStream
import java.net.URL

import scala.collection.JavaConverters._
import scala.reflect.io.{File, Path}
import scala.collection.mutable
import scala.tools.nsc.classpath.FileBasedCache
import scala.util.{Failure, Success, Try}
import scala.Try.{Failure, Success}

/** Information about a plugin loaded from a jar file.
*
Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/concurrent/Future.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.util.concurrent.{CountDownLatch, TimeUnit}
import java.util.concurrent.atomic.AtomicReference

import scala.util.control.{NonFatal, NoStackTrace}
import scala.util.{Failure, Success, Try}
import scala.Try.{Failure, Success}
import scala.concurrent.duration._
import scala.collection.BuildFrom
import scala.collection.mutable.{Builder, ArrayBuffer}
Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/concurrent/Promise.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package scala.concurrent

import scala.util.{ Try, Success, Failure }
import scala.Try.{Failure, Success}

/** Promise is an object which can be completed with a value or failed
* with an exception.
Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/concurrent/impl/Promise.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Future.InternalCallbackExecutor
import scala.concurrent.duration.Duration
import scala.annotation.{ tailrec, switch }
import scala.util.control.{ NonFatal, ControlThrowable }
import scala.util.{ Try, Success, Failure }
import scala.Try.{Failure, Success}
import scala.runtime.NonLocalReturnControl
import java.util.concurrent.locks.AbstractQueuedSynchronizer
import java.util.concurrent.atomic.AtomicReference
Expand Down
2 changes: 2 additions & 0 deletions src/library/scala/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,6 @@ package object scala {
type Right[+A, +B] = scala.util.Right[A, B]
val Right = scala.util.Right

type Try[+T] = scala.util.Try[T]
val Try = scala.util.Try
Copy link
Contributor Author

@He-Pin He-Pin Dec 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as requested.

}
16 changes: 8 additions & 8 deletions src/library/scala/util/Either.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ package util
*
* Since `Either` defines the methods `map` and `flatMap`, it can also be used in for comprehensions:
* {{{
* val right1 = Right(1) : Right[Double, Int]
* val right1 = Right(1) : Right[Double, Int]
* val right2 = Right(2)
* val right3 = Right(3)
* val left23 = Left(23.0) : Left[Double, Int]
* val left23 = Left(23.0) : Left[Double, Int]
* val left42 = Left(42.0)
*
* for {
Expand Down Expand Up @@ -370,7 +370,7 @@ sealed abstract class Either[+A, +B] extends Product with Serializable {
* rl.flatten //Either[String, Int]: Left("flounder")
* rr.flatten //Either[String, Int]: Right(7)
* }}}
*
*
* Equivalent to `flatMap(id => id)`
*/
def flatten[A1 >: A, B1](implicit ev: B <:< Either[A1, B1]): Either[A1, B1] = flatMap(ev)
Expand Down Expand Up @@ -430,8 +430,8 @@ sealed abstract class Either[+A, +B] extends Product with Serializable {
}

def toTry(implicit ev: A <:< Throwable): Try[B] = this match {
case Right(b) => Success(b)
case Left(a) => Failure(a)
case Right(b) => Try.Success(b)
case Left(a) => Try.Failure(a)
}

/** Returns `true` if this is a `Left`, `false` otherwise.
Expand Down Expand Up @@ -653,7 +653,7 @@ object Either {
case x @ Left(a) if p(a) => Some(x.asInstanceOf[Either[A, B1]])
case _ => None
}

/** Returns a `Seq` containing the `Left` value if it exists or an empty
* `Seq` if this is a `Right`.
*
Expand Down Expand Up @@ -797,7 +797,7 @@ object Either {
case Right(b) if p(b) => Some(Right(b))
case _ => None
}

/** Returns `None` if this is a `Left` or if the
* given predicate `p` does not hold for the right value,
* otherwise, returns a `Right`.
Expand All @@ -812,7 +812,7 @@ object Either {
case r @ Right(b) if p(b) => Some(r.asInstanceOf[Either[A1, B]])
case _ => None
}

/** Returns a `Seq` containing the `Right` value if
* it exists or an empty `Seq` if this is a `Left`.
*
Expand Down
146 changes: 78 additions & 68 deletions src/library/scala/util/Try.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) }
}
}
4 changes: 2 additions & 2 deletions src/library/scala/util/control/Exception.scala
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ object Exception {
/** Apply this catch logic to the supplied body, mapping the result
* into `Try[T]` - `Failure` if an exception was caught, `Success(T)` otherwise.
*/
def withTry[U >: T](body: => U): scala.util.Try[U] = toTry(Success(body))
def withTry[U >: T](body: => U): scala.util.Try[U] = toTry(Try.Success(body))

/** Create a `Catch` object with the same `isDefinedAt` logic as this one,
* but with the supplied `apply` method replacing the current one. */
Expand All @@ -269,7 +269,7 @@ object Exception {
/** Convenience methods. */
def toOption: Catch[Option[T]] = withApply(_ => None)
def toEither: Catch[Either[Throwable, T]] = withApply(Left(_))
def toTry: Catch[scala.util.Try[T]] = withApply(x => Failure(x))
def toTry: Catch[scala.util.Try[T]] = withApply(x => Try.Failure(x))
}

final val nothingCatcher: Catcher[Nothing] = mkThrowableCatcher(_ => false, throw _)
Expand Down
12 changes: 11 additions & 1 deletion src/library/scala/util/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ package object util {
* Adds chaining methods `tap` and `pipe` to every type. See [[ChainingOps]].
*/
object chaining extends ChainingSyntax
}

@deprecated("Use `Try.Failure` instead.", since = "2.13.0")
type Failure[+T] = scala.util.Try.Failure[T]
@deprecated("Use `Try.Failure` instead.", since = "2.13.0")
val Failure = scala.util.Try.Failure

@deprecated("Use `Try.Success` instead.", since = "2.13.0")
type Success[+T] = scala.util.Try.Success[T]
@deprecated("Use `Try.Success` instead.", since = "2.13.0")
val Success = scala.util.Try.Success
}
4 changes: 2 additions & 2 deletions src/partest/scala/tools/partest/nest/AbstractRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import utils.Properties._
import scala.tools.nsc.Properties.{propOrFalse, setProp, versionMsg}
import scala.collection.mutable
import scala.reflect.internal.util.Collections.distinctBy
import scala.util.{Try, Success, Failure}
import scala.Try.{Failure, Success}
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit.NANOSECONDS
Expand Down Expand Up @@ -86,7 +86,7 @@ class AbstractRunner(val config: RunnerSpec.Config, protected final val testSour
bold(cyan(s"##### Log file '${info.logFile}' from failed test #####\n")),
info.logFile.fileContents
) else Nil
val diffed =
val diffed =
if (diffOnFail) {
val differ = bold(red("% ")) + "diff "
state.transcript.find(_ startsWith differ) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import scala.language.reflectiveCalls
import scala.reflect.internal.util.ScalaClassLoader
import scala.reflect.io.File
import scala.util.Properties.{lineSeparator => EOL}
import scala.util.{Failure, Success, Try}
import scala.Try.{Failure, Success}
import Javap._
import scala.tools.nsc.interpreter.Repl

Expand Down
2 changes: 1 addition & 1 deletion src/scalacheck/org/scalacheck/Arbitrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package org.scalacheck

import language.higherKinds
import concurrent.Future
import scala.util.{Failure, Success, Try}
import scala.Try.{Failure, Success}
import scala.concurrent.duration.{Duration, FiniteDuration}

import util.Buildable
Expand Down
2 changes: 1 addition & 1 deletion src/scalacheck/org/scalacheck/Cogen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package org.scalacheck
import language.higherKinds
import scala.annotation.tailrec
import scala.collection.immutable.BitSet
import scala.util.{Failure, Success, Try}
import scala.Try.{Failure, Success}
import scala.concurrent.duration.{Duration, FiniteDuration}
import java.math.BigInteger
import rng.Seed
Expand Down
2 changes: 1 addition & 1 deletion src/scalacheck/org/scalacheck/commands/Commands.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
package org.scalacheck.commands

import org.scalacheck._
import scala.util.{Try, Success, Failure}
import scala.Try.{Failure, Success}

/** An API for stateful testing in ScalaCheck.
*
Expand Down
Loading