package util
- Alphabetic
- By Inheritance
- util
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- final class ChainingOps[A] extends AnyVal
Adds chaining methods
tapandpipeto every type. - trait ChainingSyntax extends AnyRef
- class DynamicVariable[T] extends AnyRef
DynamicVariablesprovide a binding mechanism where the current value is found through dynamic scope, but where access to the variable itself is resolved through static scope.DynamicVariablesprovide a binding mechanism where the current value is found through dynamic scope, but where access to the variable itself is resolved through static scope.The current value can be retrieved with the value method. New values should be pushed using the
withValuemethod. Values pushed viawithValueonly stay valid while thewithValue's second argument, a parameterless closure, executes. When the second argument finishes, the variable reverts to the previous value.someDynamicVariable.withValue(newValue) { // ... code called in here that calls value ... // ... will be given back the newValue ... }Each thread gets its own stack of bindings. When a new thread is created, the
DynamicVariablegets a copy of the stack of bindings from the parent thread, and from then on the bindings for the new thread are independent of those for the original thread. - sealed abstract class Either[+A, +B] extends Product with Serializable
Represents a value of one of two possible types (a disjoint union).
Represents a value of one of two possible types (a disjoint union). An instance of
Eitheris an instance of either scala.util.Left or scala.util.Right.A common use of
Eitheris as an alternative to scala.Option for dealing with possibly missing values. In this usage, scala.None is replaced with a scala.util.Left which can contain useful information. scala.util.Right takes the place of scala.Some. Convention dictates thatLeftis used for failure andRightis used for success.For example, you could use
Either[String, Int]to indicate whether a received input is aStringor anInt.import scala.io.StdIn._ val in = readLine("Type Either a string or an Int: ") val result: Either[String,Int] = try Right(in.toInt) catch { case e: NumberFormatException => Left(in) } result match { case Right(x) => s"You passed me the Int: $x, which I will increment. $x + 1 = ${x+1}" case Left(x) => s"You passed me the String: $x" }
Eitheris right-biased, which means thatRightis assumed to be the default case to operate on. If it isLeft, operations likemapandflatMapreturn theLeftvalue unchanged:def doubled(i: Int) = i * 2 Right(42).map(doubled) // Right(84) Left(42).map(doubled) // Left(42)
Since
Eitherdefines the methodsmapandflatMap, it can also be used in for comprehensions:val right1 = Right(1) : Right[Double, Int] val right2 = Right(2) val right3 = Right(3) val left23 = Left(23.0) : Left[Double, Int] val left42 = Left(42.0) for { x <- right1 y <- right2 z <- right3 } yield x + y + z // Right(6) for { x <- right1 y <- right2 z <- left23 } yield x + y + z // Left(23.0) for { x <- right1 y <- left23 z <- right2 } yield x + y + z // Left(23.0) // Guard expressions are not supported: for { i <- right1 if i > 0 } yield i // error: value withFilter is not a member of Right[Double,Int] // Similarly, refutable patterns are not supported: for (x: Int <- right1) yield x // error: value withFilter is not a member of Right[Double,Int]
Since
forcomprehensions usemapandflatMap, the types of function parameters used in the expression must be inferred. These types are constrained by theEithervalues. In particular, because of right-biasing,Leftvalues may require an explicit type argument for type parameterB, the right value. Otherwise, it might be inferred asNothing.for { x <- left23 y <- right1 z <- left42 // type at this position: Either[Double, Nothing] } yield x + y + z // ^ // error: ambiguous reference to overloaded definition, // both method + in class Int of type (x: Char)Int // and method + in class Int of type (x: Byte)Int // match argument types (Nothing) for (x <- right2 ; y <- left23) yield x + y // Left(23.0) for (x <- right2 ; y <- left42) yield x + y // error for { x <- right1 y <- left42 // type at this position: Either[Double, Nothing] z <- left23 } yield x + y + z // Left(42.0), but unexpectedly a `Either[Double,String]`
- final case class Failure[+T](exception: Throwable) extends Try[T] with Product with Serializable
- final case class Left[+A, +B](value: A) extends Either[A, B] with Product with Serializable
The left side of the disjoint union, as opposed to the scala.util.Right side.
- class Random extends Serializable
- final case class Right[+A, +B](value: B) extends Either[A, B] with Product with Serializable
The right side of the disjoint union, as opposed to the scala.util.Left side.
- final case class Success[+T](value: T) extends Try[T] with Product with Serializable
- sealed abstract class Try[+T] extends Product with Serializable
The
Trytype represents a computation that may either result in an exception, or return a successfully computed value.The
Trytype 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].For example,
Trycan 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} def divide: Try[Int] = { val dividend = Try(StdIn.readLine("Enter an Int that you'd like to divide:\n").toInt) val divisor = Try(StdIn.readLine("Enter an Int that you'd like to divide by:\n").toInt) val problem = dividend.flatMap(x => divisor.map(y => x/y)) problem match { case Success(v) => println("Result of " + dividend.get + "/"+ divisor.get +" is: " + v) Success(v) case Failure(e) => println("You must've divided by zero or entered something that's not an Int. Try again!") println("Info from the exception: " + e.getMessage) divide } }
An important property of
Tryshown in the above example is its ability to pipeline, or chain, operations, catching exceptions along the way. TheflatMapandmapcombinators in the above example each essentially pass off either their successfully completed value, wrapped in theSuccesstype for it to be further operated upon by the next combinator in the chain, or the exception wrapped in theFailuretype usually to be simply passed on down the chain. Combinators such asrecoverandrecoverWithare designed to provide some type of default behavior in the case of failure.Note: only non-fatal exceptions are caught by the combinators on
Try(see scala.util.control.NonFatal). Serious system errors, on the other hand, will be thrown.Note:: all Try combinators will catch exceptions and return failure unless otherwise specified in the documentation.
Trycomes to the Scala standard library after years of use as an integral part of Twitter's stack.
Value Members
- object Either extends java.io.Serializable
- object Properties extends PropertiesTrait
Loads
library.propertiesfrom the jar. - object Random extends Random
The object
Randomoffers a default implementation of scala.util.Random and random-related convenience methods. - object Sorting
The
Sortingobject provides convenience wrappers forjava.util.Arrays.sort.The
Sortingobject provides convenience wrappers forjava.util.Arrays.sort. Methods that defer tojava.util.Arrays.sortsay that they do or under what conditions that they do.Sortingalso implements a general-purpose quicksort and stable (merge) sort for those cases wherejava.util.Arrays.sortcould only be used at the cost of a large memory penalty. If performance rather than memory usage is the primary concern, one may wish to find alternate strategies to usejava.util.Arrays.sortdirectly e.g. by boxing primitives to use a custom ordering on them.Sortingprovides methods where you can provide a comparison function, or can request a sort of items that are scala.math.Ordered or that otherwise have an implicit or explicit scala.math.Ordering.Note also that high-performance non-default sorts for numeric types are not provided. If this is required, it is advisable to investigate other libraries that cover this use case.
- object Try extends java.io.Serializable
- object Using
A utility for performing automatic resource management.
A utility for performing automatic resource management. It can be used to perform an operation using resources, after which it releases the resources in reverse order of their creation.
Usage
There are multiple ways to automatically manage resources with
Using. If you only need to manage a single resource, theapplymethod is easiest; it wraps the resource opening, operation, and resource releasing in aTry.Example:
import java.io.{BufferedReader, FileReader} import scala.util.{Try, Using} val lines: Try[Seq[String]] = Using(new BufferedReader(new FileReader("file.txt"))) { reader => Iterator.continually(reader.readLine()).takeWhile(_ != null).toSeq }
If you need to manage multiple resources,
Using.Managershould be used. It allows the managing of arbitrarily many resources, whose creation, use, and release are all wrapped in aTry.Example:
import java.io.{BufferedReader, FileReader} import scala.util.{Try, Using} val lines: Try[Seq[String]] = Using.Manager { use => val r1 = use(new BufferedReader(new FileReader("file1.txt"))) val r2 = use(new BufferedReader(new FileReader("file2.txt"))) val r3 = use(new BufferedReader(new FileReader("file3.txt"))) val r4 = use(new BufferedReader(new FileReader("file4.txt"))) // use your resources here def lines(reader: BufferedReader): Iterator[String] = Iterator.continually(reader.readLine()).takeWhile(_ != null) (lines(r1) ++ lines(r2) ++ lines(r3) ++ lines(r4)).toList }
If you wish to avoid wrapping management and operations in a
Try, you can useUsing.resource, which throws any exceptions that occur.Example:
import java.io.{BufferedReader, FileReader} import scala.util.Using val lines: Seq[String] = Using.resource(new BufferedReader(new FileReader("file.txt"))) { reader => Iterator.continually(reader.readLine()).takeWhile(_ != null).toSeq }
Suppression Behavior
If two exceptions are thrown (e.g., by an operation and closing a resource), one of them is re-thrown, and the other is added to it as a suppressed exception. If the two exceptions are of different 'severities' (see below), the one of a higher severity is re-thrown, and the one of a lower severity is added to it as a suppressed exception. If the two exceptions are of the same severity, the one thrown first is re-thrown, and the one thrown second is added to it as a suppressed exception. If an exception is a
ControlThrowable, or if it does not support suppression (seeThrowable's constructor with anenableSuppressionparameter), an exception that would have been suppressed is instead discarded.Exceptions are ranked from highest to lowest severity as follows:
java.lang.VirtualMachineErrorjava.lang.LinkageErrorjava.lang.InterruptedExceptionandjava.lang.ThreadDeath- fatal exceptions, excluding
scala.util.control.ControlThrowable scala.util.control.ControlThrowable- all other exceptions
When more than two exceptions are thrown, the first two are combined and re-thrown as described above, and each successive exception thrown is combined as it is thrown.
- object chaining extends ChainingSyntax
Adds chaining methods
tapandpipeto every type.Adds chaining methods
tapandpipeto every type. See ChainingOps.
This is the documentation for the Scala standard library.
Package structure
The scala package contains core types like
Int,Float,ArrayorOptionwhich are accessible in all Scala compilation units without explicit qualification or imports.Notable packages include:
scala.collectionand its sub-packages contain Scala's collections frameworkscala.collection.immutable- Immutable, sequential data-structures such asVector,List,Range,HashMaporHashSetscala.collection.mutable- Mutable, sequential data-structures such asArrayBuffer,StringBuilder,HashMaporHashSetscala.collection.concurrent- Mutable, concurrent data-structures such asTrieMapscala.concurrent- Primitives for concurrent programming such asFuturesandPromisesscala.io- Input and output operationsscala.math- Basic math functions and additional numeric types likeBigIntandBigDecimalscala.sys- Interaction with other processes and the operating systemscala.util.matching- Regular expressionsOther packages exist. See the complete list on the right.
Additional parts of the standard library are shipped as separate libraries. These include:
scala.reflect- Scala's reflection API (scala-reflect.jar)scala.xml- XML parsing, manipulation, and serialization (scala-xml.jar)scala.collection.parallel- Parallel collections (scala-parallel-collections.jar)scala.util.parsing- Parser combinators (scala-parser-combinators.jar)scala.swing- A convenient wrapper around Java's GUI framework called Swing (scala-swing.jar)Automatic imports
Identifiers in the scala package and the
scala.Predefobject are always in scope by default.Some of these identifiers are type aliases provided as shortcuts to commonly used classes. For example,
Listis an alias forscala.collection.immutable.List.Other aliases refer to classes provided by the underlying platform. For example, on the JVM,
Stringis an alias forjava.lang.String.