object Predef extends LowPriorityImplicits with DeprecatedPredef
The Predef
object provides definitions that are accessible in all Scala
compilation units without explicit qualification.
Commonly Used Types
Predef provides type aliases for types which are commonly used, such as the immutable collection types scala.collection.immutable.Map, scala.collection.immutable.Set, and the scala.collection.immutable.List constructors (scala.collection.immutable.:: and scala.collection.immutable.Nil).
Console Output
For basic console output, Predef
provides convenience methods print and println,
which are aliases of the methods in the object scala.Console.
Assertions
A set of assert
functions are provided for use as a way to document
and dynamically check invariants in code. Invocations of assert
can be elided
at compile time by providing the command line option -Xdisable-assertions
,
which raises -Xelide-below
above elidable.ASSERTION
, to the scalac
command.
Variants of assert
intended for use with static analysis tools are also
provided: assume
, require
and ensuring
. require
and ensuring
are
intended for use as a means of design-by-contract style specification
of pre- and post-conditions on functions, with the intention that these
specifications could be consumed by a static analysis tool. For instance,
def addNaturals(nats: List[Int]): Int = { require(nats forall (_ >= 0), "List contains negative numbers") nats.foldLeft(0)(_ + _) } ensuring(_ >= 0)
The declaration of addNaturals
states that the list of integers passed should
only contain natural numbers (i.e. non-negative), and that the result returned
will also be natural. require
is distinct from assert
in that if the
condition fails, then the caller of the function is to blame rather than a
logical error having been made within addNaturals
itself. ensuring
is a
form of assert
that declares the guarantee the function is providing with
regards to its return value.
Implicit Conversions
A number of commonly applied implicit conversions are also defined here, and in the parent type scala.LowPriorityImplicits. Implicit conversions are provided for the "widening" of numeric values, for instance, converting a Short value to a Long value as required, and to add additional higher-order functions to Array values. These are described in more detail in the documentation of scala.Array.
- Source
- Predef.scala
- Grouped
- Alphabetic
- By Inheritance
- Predef
- DeprecatedPredef
- LowPriorityImplicits
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Utility Methods
-
def
???: Nothing
???
can be used for marking methods that remain to be implemented.???
can be used for marking methods that remain to be implemented.- Exceptions thrown
-
def
classOf[T]: Class[T]
Retrieve the runtime representation of a class type.
Retrieve the runtime representation of a class type.
classOf[T]
is equivalent to the class literalT.class
in Java.val listClass = classOf[List[_]] // listClass is java.lang.Class[List[_]] = class scala.collection.immutable.List val mapIntString = classOf[Map[Int,String]] // mapIntString is java.lang.Class[Map[Int,String]] = interface scala.collection.immutable.Map
Example: -
def
identity[A](x: A): A
- Annotations
- @inline()
-
def
implicitly[T](implicit e: T): T
- Annotations
- @inline()
-
def
locally[T](x: T): T
- Annotations
- @inline()
Assertions
These methods support program verification and runtime correctness.
-
final
def
assert(assertion: Boolean, message: ⇒ Any): Unit
Tests an expression, throwing an
AssertionError
if false. -
def
assert(assertion: Boolean): Unit
Tests an expression, throwing an
AssertionError
if false. -
final
def
assume(assumption: Boolean, message: ⇒ Any): Unit
Tests an expression, throwing an
AssertionError
if false.Tests an expression, throwing an
AssertionError
if false. This method differs from assert only in the intent expressed: assert contains a predicate which needs to be proven, while assume contains an axiom for a static checker. Calls to this method will not be generated if-Xelide-below
is greater thanASSERTION
.- assumption
the expression to test
- message
a String to include in the failure message
-
def
assume(assumption: Boolean): Unit
Tests an expression, throwing an
AssertionError
if false.Tests an expression, throwing an
AssertionError
if false. This method differs from assert only in the intent expressed: assert contains a predicate which needs to be proven, while assume contains an axiom for a static checker. Calls to this method will not be generated if-Xelide-below
is greater thanASSERTION
.- assumption
the expression to test
-
final
def
require(requirement: Boolean, message: ⇒ Any): Unit
Tests an expression, throwing an
IllegalArgumentException
if false.Tests an expression, throwing an
IllegalArgumentException
if false. This method is similar toassert
, but blames the caller of the method for violating the condition.- requirement
the expression to test
- message
a String to include in the failure message
- Annotations
- @inline()
-
def
require(requirement: Boolean): Unit
Tests an expression, throwing an
IllegalArgumentException
if false.Tests an expression, throwing an
IllegalArgumentException
if false. This method is similar toassert
, but blames the caller of the method for violating the condition.- requirement
the expression to test
Console Output
These methods provide output via the console.
-
def
print(x: Any): Unit
Prints an object to
out
using itstoString
method.Prints an object to
out
using itstoString
method.- x
the object to print; may be null.
-
def
printf(text: String, xs: Any*): Unit
Prints its arguments as a formatted string to the default output, based on a string pattern (in a fashion similar to printf in C).
Prints its arguments as a formatted string to the default output, based on a string pattern (in a fashion similar to printf in C).
The interpretation of the formatting patterns is described in java.util.Formatter.
Consider using the f interpolator as more type safe and idiomatic.
- text
the pattern for formatting the arguments.
- Exceptions thrown
java.lang.IllegalArgumentException
if there was a problem with the format string or arguments- See also
-
def
println(x: Any): Unit
Prints out an object to the default output, followed by a newline character.
Prints out an object to the default output, followed by a newline character.
- x
the object to print.
-
def
println(): Unit
Prints a newline character on the default output.
Type Constraints
These entities allows constraints between types to be stipulated.
-
sealed abstract
class
<:<[-From, +To] extends (From) ⇒ To with Serializable
An instance of
A <:< B
witnesses thatA
is a subtype ofB
.An instance of
A <:< B
witnesses thatA
is a subtype ofB
. Requiring an implicit argument of the typeA <:< B
encodes the generalized constraintA <: B
.- Annotations
- @implicitNotFound( msg = ... )
- Note
we need a new type constructor
<:<
and evidenceconforms
, as reusingFunction1
andidentity
leads to ambiguities in case of type errors (any2stringadd
is inferred) To constrain any abstract type T that's in scope in a method's argument list (not just the method's own type parameters) simply add an implicit argument of typeT <:< U
, whereU
is the required upper bound; or for lower-bounds, use:L <:< T
, whereL
is the required lower bound. In part contributed by Jason Zaugg.
-
sealed abstract
class
=:=[From, To] extends (From) ⇒ To with Serializable
An instance of
A =:= B
witnesses that the typesA
andB
are equal.An instance of
A =:= B
witnesses that the typesA
andB
are equal.- Annotations
- @implicitNotFound( msg = ... )
- See also
<:<
for expressing subtyping constraints
- implicit def $conforms[A]: <:<[A, A]
- object =:= extends Serializable
Aliases
These aliases bring selected immutable types into scope without any imports.
- type Class[T] = java.lang.Class[T]
- type Function[-A, +B] = (A) ⇒ B
- type Map[A, +B] = collection.immutable.Map[A, B]
- type Set[A] = collection.immutable.Set[A]
-
type
String = java.lang.String
The
String
type in Scala has methods that come either from the underlying Java String (see the documentation corresponding to your Java version, for example http://docs.oracle.com/javase/8/docs/api/java/lang/String.html) or are added implicitly through scala.collection.immutable.StringOps.
- val Map: collection.immutable.Map.type
- val Set: collection.immutable.Set.type
String Conversions
Conversions to and from String and StringOps.
-
implicit
def
augmentString(x: String): StringOps
- Annotations
- @inline()
-
implicit
def
unaugmentString(x: StringOps): String
- Annotations
- @inline()
-
implicit
def
unwrapString(ws: WrappedString): String
- Definition Classes
- LowPriorityImplicits
-
implicit
def
wrapString(s: String): WrappedString
- Definition Classes
- LowPriorityImplicits
Implicit Classes
These implicit classes add useful extension methods to every type.
- implicit final class ArrowAssoc[A] extends AnyVal
- implicit final class Ensuring[A] extends AnyVal
- implicit final class StringFormat[A] extends AnyVal
- implicit final class any2stringadd[A] extends AnyVal
CharSequence Wrappers
Wrappers that implements CharSequence and were implicit classes.
- final class SeqCharSequence extends CharSequence
-
final
class
ArrayCharSequence extends CharSequence
- Annotations
- @deprecated
- Deprecated
(Since version 2.12.13) use
java.nio.CharBuffer.wrap
instead
- def ArrayCharSequence(arrayOfChars: Array[Char]): ArrayCharSequence
- def SeqCharSequence(sequenceOfChars: collection.IndexedSeq[Char]): SeqCharSequence
Java to Scala
Implicit conversion from Java primitive wrapper types to Scala equivalents.
- implicit def Boolean2boolean(x: java.lang.Boolean): Boolean
- implicit def Byte2byte(x: java.lang.Byte): Byte
- implicit def Character2char(x: Character): Char
- implicit def Double2double(x: java.lang.Double): Double
- implicit def Float2float(x: java.lang.Float): Float
- implicit def Integer2int(x: Integer): Int
- implicit def Long2long(x: java.lang.Long): Long
- implicit def Short2short(x: java.lang.Short): Short
Scala to Java
Implicit conversion from Scala AnyVals to Java primitive wrapper types equivalents.
- implicit def boolean2Boolean(x: Boolean): java.lang.Boolean
- implicit def byte2Byte(x: Byte): java.lang.Byte
- implicit def char2Character(x: Char): Character
- implicit def double2Double(x: Double): java.lang.Double
- implicit def float2Float(x: Float): java.lang.Float
- implicit def int2Integer(x: Int): Integer
- implicit def long2Long(x: Long): java.lang.Long
- implicit def short2Short(x: Short): java.lang.Short
Array to WrappedArray
Conversions from Arrays to WrappedArrays.
-
implicit
def
genericWrapArray[T](xs: Array[T]): WrappedArray[T]
- Definition Classes
- LowPriorityImplicits
-
implicit
def
wrapBooleanArray(xs: Array[Boolean]): WrappedArray[Boolean]
- Definition Classes
- LowPriorityImplicits
-
implicit
def
wrapByteArray(xs: Array[Byte]): WrappedArray[Byte]
- Definition Classes
- LowPriorityImplicits
-
implicit
def
wrapCharArray(xs: Array[Char]): WrappedArray[Char]
- Definition Classes
- LowPriorityImplicits
-
implicit
def
wrapDoubleArray(xs: Array[Double]): WrappedArray[Double]
- Definition Classes
- LowPriorityImplicits
-
implicit
def
wrapFloatArray(xs: Array[Float]): WrappedArray[Float]
- Definition Classes
- LowPriorityImplicits
-
implicit
def
wrapIntArray(xs: Array[Int]): WrappedArray[Int]
- Definition Classes
- LowPriorityImplicits
-
implicit
def
wrapLongArray(xs: Array[Long]): WrappedArray[Long]
- Definition Classes
- LowPriorityImplicits
-
implicit
def
wrapRefArray[T <: AnyRef](xs: Array[T]): WrappedArray[T]
- Definition Classes
- LowPriorityImplicits
-
implicit
def
wrapShortArray(xs: Array[Short]): WrappedArray[Short]
- Definition Classes
- LowPriorityImplicits
-
implicit
def
wrapUnitArray(xs: Array[Unit]): WrappedArray[Unit]
- Definition Classes
- LowPriorityImplicits
Ungrouped
-
class
DummyImplicit extends AnyRef
A type for which there is always an implicit value.
A type for which there is always an implicit value.
- See also
scala.Array$, method
fallbackCanBuildFrom
-
type
Manifest[T] = reflect.Manifest[T]
- Annotations
- @implicitNotFound( msg = "No Manifest available for ${T}." )
- type OptManifest[T] = reflect.OptManifest[T]
- implicit final class RichException extends AnyVal
-
type
ClassManifest[T] = ClassTag[T]
- Annotations
- @implicitNotFound( msg = ... ) @deprecated
- Deprecated
(Since version 2.10.0) use
scala.reflect.ClassTag
instead
-
type
Pair[+A, +B] = (A, B)
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use built-in tuple syntax or Tuple2 instead
-
type
Triple[+A, +B, +C] = (A, B, C)
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use built-in tuple syntax or Tuple3 instead
- val Manifest: ManifestFactory.type
- val NoManifest: reflect.NoManifest.type
- implicit val StringCanBuildFrom: CanBuildFrom[String, Char, String]
- implicit def booleanArrayOps(xs: Array[Boolean]): ofBoolean
-
implicit
def
booleanWrapper(x: Boolean): RichBoolean
- Definition Classes
- LowPriorityImplicits
- Annotations
- @inline()
- implicit def byteArrayOps(xs: Array[Byte]): ofByte
-
implicit
def
byteWrapper(x: Byte): RichByte
We prefer the java.lang.* boxed types to these wrappers in any potential conflicts.
We prefer the java.lang.* boxed types to these wrappers in any potential conflicts. Conflicts do exist because the wrappers need to implement ScalaNumber in order to have a symmetric equals method, but that implies implementing java.lang.Number as well.
Note - these are inlined because they are value classes, but the call to xxxWrapper is not eliminated even though it does nothing. Even inlined, every call site does a no-op retrieval of Predef's MODULE$ because maybe loading Predef has side effects!
- Definition Classes
- LowPriorityImplicits
- Annotations
- @inline()
- implicit def charArrayOps(xs: Array[Char]): ofChar
-
implicit
def
charWrapper(c: Char): RichChar
- Definition Classes
- LowPriorityImplicits
- Annotations
- @inline()
- implicit def doubleArrayOps(xs: Array[Double]): ofDouble
-
implicit
def
doubleWrapper(x: Double): RichDouble
- Definition Classes
- LowPriorityImplicits
- Annotations
- @inline()
-
implicit
def
fallbackStringCanBuildFrom[T]: CanBuildFrom[String, T, collection.immutable.IndexedSeq[T]]
- Definition Classes
- LowPriorityImplicits
- implicit def floatArrayOps(xs: Array[Float]): ofFloat
-
implicit
def
floatWrapper(x: Float): RichFloat
- Definition Classes
- LowPriorityImplicits
- Annotations
- @inline()
- implicit def genericArrayOps[T](xs: Array[T]): ArrayOps[T]
- implicit def intArrayOps(xs: Array[Int]): ofInt
-
implicit
def
intWrapper(x: Int): RichInt
- Definition Classes
- LowPriorityImplicits
- Annotations
- @inline()
- implicit def longArrayOps(xs: Array[Long]): ofLong
-
implicit
def
longWrapper(x: Long): RichLong
- Definition Classes
- LowPriorityImplicits
- Annotations
- @inline()
- def manifest[T](implicit m: Manifest[T]): Manifest[T]
- def optManifest[T](implicit m: OptManifest[T]): OptManifest[T]
- implicit def refArrayOps[T <: AnyRef](xs: Array[T]): ofRef[T]
- implicit def shortArrayOps(xs: Array[Short]): ofShort
-
implicit
def
shortWrapper(x: Short): RichShort
- Definition Classes
- LowPriorityImplicits
- Annotations
- @inline()
- implicit def tuple2ToZippedOps[T1, T2](x: (T1, T2)): Ops[T1, T2]
- implicit def tuple3ToZippedOps[T1, T2, T3](x: (T1, T2, T3)): Ops[T1, T2, T3]
- implicit def unitArrayOps(xs: Array[Unit]): ofUnit
- object DummyImplicit
-
val
ClassManifest: ClassManifestFactory.type
- Annotations
- @deprecated
- Deprecated
(Since version 2.10.0) use
scala.reflect.ClassTag
instead
-
def
any2ArrowAssoc[A](x: A): ArrowAssoc[A]
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use
ArrowAssoc
-
def
any2Ensuring[A](x: A): Ensuring[A]
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use
Ensuring
-
def
any2stringfmt(x: Any): StringFormat[Any]
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use
StringFormat
-
def
arrayToCharSequence(xs: Array[Char]): CharSequence
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use
java.nio.CharBuffer.wrap
-
def
classManifest[T](implicit m: ClassManifest[T]): ClassManifest[T]
- Annotations
- @deprecated
- Deprecated
(Since version 2.10.0) use scala.reflect.classTag[T] instead
-
def
conforms[A]: <:<[A, A]
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use
implicitly[T <:< U]
oridentity
instead.
-
def
exceptionWrapper(exc: scala.Throwable): RichException
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use
Throwable
directly
-
def
readBoolean(): Boolean
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readByte(): Byte
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readChar(): Char
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readDouble(): Double
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readFloat(): Float
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readInt(): Int
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readLine(text: String, args: Any*): String
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readLine(): String
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readLong(): Long
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readShort(): Short
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readf(format: String): List[Any]
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readf1(format: String): Any
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readf2(format: String): (Any, Any)
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
readf3(format: String): (Any, Any, Any)
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use the method in
scala.io.StdIn
-
def
seqToCharSequence(xs: collection.IndexedSeq[Char]): CharSequence
- Definition Classes
- DeprecatedPredef
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use
SeqCharSequence
-
object
Pair
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use built-in tuple syntax or Tuple2 instead
-
object
Triple
- Annotations
- @deprecated
- Deprecated
(Since version 2.11.0) use built-in tuple syntax or Tuple3 instead
This is the documentation for the Scala standard library.
Package structure
The scala package contains core types like
Int
,Float
,Array
orOption
which are accessible in all Scala compilation units without explicit qualification or imports.Notable packages include:
scala.collection
and its sub-packages contain Scala's collections frameworkscala.collection.immutable
- Immutable, sequential data-structures such asVector
,List
,Range
,HashMap
orHashSet
scala.collection.mutable
- Mutable, sequential data-structures such asArrayBuffer
,StringBuilder
,HashMap
orHashSet
scala.collection.concurrent
- Mutable, concurrent data-structures such asTrieMap
scala.collection.parallel.immutable
- Immutable, parallel data-structures such asParVector
,ParRange
,ParHashMap
orParHashSet
scala.collection.parallel.mutable
- Mutable, parallel data-structures such asParArray
,ParHashMap
,ParTrieMap
orParHashSet
scala.concurrent
- Primitives for concurrent programming such asFutures
andPromises
scala.io
- Input and output operationsscala.math
- Basic math functions and additional numeric types likeBigInt
andBigDecimal
scala.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.swing
- A convenient wrapper around Java's GUI framework called Swing (scala-swing.jar)scala.util.parsing
- Parser combinators (scala-parser-combinators.jar)Automatic imports
Identifiers in the scala package and the
scala.Predef
object are always in scope by default.Some of these identifiers are type aliases provided as shortcuts to commonly used classes. For example,
List
is an alias forscala.collection.immutable.List
.Other aliases refer to classes provided by the underlying platform. For example, on the JVM,
String
is an alias forjava.lang.String
.