Predef

scala.Predef
object Predef

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 and scala.collection.immutable.Set.

=== 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.

Attributes

Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Predef.type

Members list

Grouped members

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.

Attributes

Throws
NotImplementedError

when ??? is invoked.

def classOf[T]: Class[T]

Retrieve the runtime representation of a class type. classOf[T] is equivalent to the class literal T.class in Java.

Retrieve the runtime representation of a class type. classOf[T] is equivalent to the class literal T.class in Java.

Attributes

Returns

The runtime Class representation of type T.

Example
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
def identity[A](x: A): A

A method that returns its input value.

A method that returns its input value.

Type parameters

A

type of the input value x.

Value parameters

x

the value of type A to be returned.

Attributes

Returns

the value x.

def implicitly[T](implicit e: T): T

Summon an implicit value of type T. Usually, the argument is not passed explicitly.

Summon an implicit value of type T. Usually, the argument is not passed explicitly.

Type parameters

T

the type of the value to be summoned

Attributes

Returns

the implicit value of type T

def locally[T](x: T): T

Used to mark code blocks as being expressions, instead of being taken as part of anonymous classes and the like. This is just a different name for identity.

Used to mark code blocks as being expressions, instead of being taken as part of anonymous classes and the like. This is just a different name for identity.

Attributes

Example

Separating code blocks from new:

           val x = new AnyRef
           {
             val y = ...
             println(y)
           }
           // the { ... } block is seen as the body of an anonymous class
           val x = new AnyRef
           {
             val y = ...
             println(y)
           }
           // an empty line is a brittle "fix"
           val x = new AnyRef
           locally {
             val y = ...
             println(y)
           }
           // locally guards the block and helps communicate intent
def valueOf[T](implicit vt: ValueOf[T]): T

Retrieve the single value of a type with a unique inhabitant.

Retrieve the single value of a type with a unique inhabitant.

Attributes

Example
object Foo
val foo = valueOf[Foo.type]
// foo is Foo.type = Foo
val bar = valueOf[23]
// bar is 23.type = 23
inline def valueOf[T]: T

Retrieve the single value of a type with a unique inhabitant.

Retrieve the single value of a type with a unique inhabitant.

Attributes

Example
object Foo
val foo = valueOf[Foo.type]
// foo is Foo.type = Foo
val bar = valueOf[23]
// bar is 23.type = 23

Assertions

These methods support program verification and runtime correctness.

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 than ASSERTION.

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 than ASSERTION.

Value parameters

assumption

the expression to test

Attributes

See also
final def assume(assumption: Boolean, message: => Any): Unit

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 than ASSERTION.

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 than ASSERTION.

Value parameters

assumption

the expression to test

message

a String to include in the failure message

Attributes

See also

Tests an expression, throwing an IllegalArgumentException if false. This method is similar to assert, but blames the caller of the method for violating the condition.

Tests an expression, throwing an IllegalArgumentException if false. This method is similar to assert, but blames the caller of the method for violating the condition.

Value parameters

requirement

the expression to test

Attributes

final def require(requirement: Boolean, message: => Any): Unit

Tests an expression, throwing an IllegalArgumentException if false. This method is similar to assert, but blames the caller of the method for violating the condition.

Tests an expression, throwing an IllegalArgumentException if false. This method is similar to assert, but blames the caller of the method for violating the condition.

Value parameters

message

a String to include in the failure message

requirement

the expression to test

Attributes

Console Output

These methods provide output via the console.

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.

Value parameters

text

the pattern for formatting the arguments.

xs

the arguments used to instantiate the pattern.

Attributes

Throws
java.lang.IllegalArgumentException

if there was a problem with the format string or arguments

See also
def println(): Unit

Prints a newline character on the default output.

Prints a newline character on the default output.

Attributes

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.

Value parameters

x

the object to print.

Attributes

Aliases

These aliases bring selected immutable types into scope without any imports.

val ->: Tuple2.type

Allows destructuring tuples with the same syntax as constructing them.

Allows destructuring tuples with the same syntax as constructing them.

Attributes

Example
val tup = "foobar" -> 3
val c = tup match {
 case str -> i => str.charAt(i)
}
type Class[T] = Class[T]

Attributes

type Function[-A, +B] = A => B

Attributes

type Map[K, +V] = Map[K, V]

Attributes

val Map: Map.type

Attributes

type Set[A] = Set[A]

Attributes

val Set: Set.type

Attributes

type String = String

The String type in Scala has all the methods of the underlying java.lang.String, of which it is just an alias.

The String type in Scala has all the methods of the underlying java.lang.String, of which it is just an alias.

In addition, extension methods in scala.collection.StringOps are added implicitly through the conversion augmentString.

Attributes

String Conversions

Conversions from String to StringOps or WrappedString.

implicit def augmentString(x: String): StringOps

Attributes

implicit def wrapString(s: String): WrappedString

Attributes

Inherited from:
LowPriorityImplicits (hidden)

Implicit Classes

These implicit classes add useful extension methods to every type.

final implicit class ArrowAssoc[A](self: A) extends AnyVal

Attributes

Supertypes
class AnyVal
trait Matchable
class Any
final implicit def ArrowAssoc[A](self: A): ArrowAssoc[A]

Attributes

final implicit class Ensuring[A](self: A) extends AnyVal

Attributes

Supertypes
class AnyVal
trait Matchable
class Any
final implicit def Ensuring[A](self: A): Ensuring[A]

Attributes

final implicit class StringFormat[A](self: A) extends AnyVal

Attributes

Supertypes
class AnyVal
trait Matchable
class Any
final implicit def StringFormat[A](self: A): StringFormat[A]

Attributes

final implicit class any2stringadd[A](self: A) extends AnyVal

Injects String concatenation operator + to any classes.

Injects String concatenation operator + to any classes.

Attributes

Deprecated
[Since version 2.13.0] Implicit injection of + is deprecated. Convert to String to call +
Supertypes
class AnyVal
trait Matchable
class Any
final implicit def any2stringadd[A](self: A): any2stringadd[A]

Injects String concatenation operator + to any classes.

Injects String concatenation operator + to any classes.

Attributes

Deprecated
[Since version 2.13.0] Implicit injection of + is deprecated. Convert to String to call +

CharSequence Wrappers

Wrappers that implements CharSequence and were implicit classes.

Attributes

Supertypes
trait CharSequence
class Object
trait Matchable
class Any

Attributes

Supertypes
trait CharSequence
class Object
trait Matchable
class Any

Java to Scala

Implicit conversion from Java primitive wrapper types to Scala equivalents.

implicit def Boolean2boolean(x: Boolean): Boolean

Attributes

implicit def Byte2byte(x: Byte): Byte

Attributes

implicit def Character2char(x: Character): Char

Attributes

implicit def Double2double(x: Double): Double

Attributes

implicit def Float2float(x: Float): Float

Attributes

implicit def Integer2int(x: Integer): Int

Attributes

implicit def Long2long(x: Long): Long

Attributes

implicit def Short2short(x: Short): Short

Attributes

Scala to Java

Implicit conversion from Scala AnyVals to Java primitive wrapper types equivalents.

implicit def boolean2Boolean(x: Boolean): Boolean

Attributes

implicit def byte2Byte(x: Byte): Byte

Attributes

implicit def char2Character(x: Char): Character

Attributes

implicit def double2Double(x: Double): Double

Attributes

implicit def float2Float(x: Float): Float

Attributes

implicit def int2Integer(x: Int): Integer

Attributes

implicit def long2Long(x: Long): Long

Attributes

implicit def short2Short(x: Short): Short

Attributes

Array to ArraySeq

Conversions from Arrays to ArraySeqs.

implicit def genericWrapArray[T](xs: Array[T]): ArraySeq[T]

Attributes

Inherited from:
LowPriorityImplicits (hidden)

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def wrapByteArray(xs: Array[Byte]): ofByte

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def wrapCharArray(xs: Array[Char]): ofChar

Attributes

Inherited from:
LowPriorityImplicits (hidden)

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def wrapFloatArray(xs: Array[Float]): ofFloat

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def wrapIntArray(xs: Array[Int]): ofInt

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def wrapLongArray(xs: Array[Long]): ofLong

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def wrapRefArray[T <: AnyRef | Null](xs: Array[T]): ofRef[T]

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def wrapShortArray(xs: Array[Short]): ofShort

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def wrapUnitArray(xs: Array[Unit]): ofUnit

Attributes

Inherited from:
LowPriorityImplicits (hidden)

Type members

Types

type Manifest[T] = Manifest[T]
infix type is[A <: AnyKind, B] = B { type Self = A; }

A type supporting Self-based type classes.

A type supporting Self-based type classes.

A is TC

expands to

TC { type Self = A }

which is what is needed for a context bound [A: TC].

Attributes

Value members

Concrete methods

transparent inline def assert(inline assertion: Boolean, inline message: => Any): Unit
transparent inline def assert(inline assertion: Boolean): Unit
def manifest[T](implicit m: Manifest[T]): Manifest[T]
def optManifest[T](implicit m: OptManifest[T]): OptManifest[T]
transparent inline def summon[T](using x: T): x.type

Summon a given value of type T. Usually, the argument is not passed explicitly.

Summon a given value of type T. Usually, the argument is not passed explicitly.

Type parameters

T

the type of the value to be summoned

Attributes

Returns

the given value typed: the provided type parameter

Concrete fields

val Manifest: Manifest.type

Extensions

Extensions

extension [T](x: T | Null)
inline def nn: x.type & T

Strips away the nullability from a value. Note that .nn performs a checked cast, so if invoked on a null value it will throw an NullPointerException.

Strips away the nullability from a value. Note that .nn performs a checked cast, so if invoked on a null value it will throw an NullPointerException.

Attributes

Example
val s1: String | Null = "hello"
val s2: String = s1.nn
val s3: String | Null = null
val s4: String = s3.nn // throw NullPointerException
extension (inline x: AnyRef | Null)
infix inline def eq(inline y: AnyRef | Null): Boolean

Enables an expression of type T|Null, where T is a subtype of AnyRef, to be checked for null using eq rather than only ==. This is needed because Null no longer has eq or ne methods, only == and != inherited from Any.

Enables an expression of type T|Null, where T is a subtype of AnyRef, to be checked for null using eq rather than only ==. This is needed because Null no longer has eq or ne methods, only == and != inherited from Any.

Attributes

infix inline def ne(inline y: AnyRef | Null): Boolean

Enables an expression of type T|Null, where T is a subtype of AnyRef, to be checked for null using ne rather than only !=. This is needed because Null no longer has eq or ne methods, only == and != inherited from Any.

Enables an expression of type T|Null, where T is a subtype of AnyRef, to be checked for null using ne rather than only !=. This is needed because Null no longer has eq or ne methods, only == and != inherited from Any.

Attributes

extension [T](x: T)
inline def runtimeChecked: x.type

Asserts that a term should be exempt from static checks that can be reliably checked at runtime.

Asserts that a term should be exempt from static checks that can be reliably checked at runtime.

Attributes

Example
val xs: Option[Int] = Option(1)
xs.runtimeChecked match
  case Some(x) => x // `Some(_)` can be checked at runtime, so no warning
val xs: List[Int] = List(1,2,3)
val y :: ys = xs.runtimeChecked // `_ :: _` can be checked at runtime, so no warning

Implicits

Implicits

implicit def $conforms[A]: A => A

An implicit of type A => A is available for all A because it can always be implemented using the identity function. This also means that an implicit of type A => B is always available when A <: B, because (A => A) <: (A => B).

An implicit of type A => A is available for all A because it can always be implemented using the identity function. This also means that an implicit of type A => B is always available when A <: B, because (A => A) <: (A => B).

Attributes

implicit def byteArrayOps(xs: Array[Byte]): ArrayOps[Byte]
implicit def charArrayOps(xs: Array[Char]): ArrayOps[Char]
implicit def genericArrayOps[T](xs: Array[T]): ArrayOps[T]
implicit def intArrayOps(xs: Array[Int]): ArrayOps[Int]
implicit def longArrayOps(xs: Array[Long]): ArrayOps[Long]
implicit def refArrayOps[T <: AnyRef | Null](xs: Array[T]): ArrayOps[T]
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]): ArrayOps[Unit]

Inherited implicits

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def byteWrapper(x: Byte): RichByte

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.

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!

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def charWrapper(c: Char): RichChar

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def doubleWrapper(x: Double): RichDouble

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def floatWrapper(x: Float): RichFloat

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def intWrapper(x: Int): RichInt

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def longWrapper(x: Long): RichLong

Attributes

Inherited from:
LowPriorityImplicits (hidden)
implicit def shortWrapper(x: Short): RichShort

Attributes

Inherited from:
LowPriorityImplicits (hidden)

Deprecated and Inherited implicits

Attributes

Deprecated
[Since version 2.13.0] implicit conversions from Array to immutable.IndexedSeq are implemented by copying; use `toIndexedSeq` explicitly if you want to copy, or use the more efficient non-copying ArraySeq.unsafeWrapArray
Inherited from:
LowPriorityImplicits2 (hidden)