Packages

o

scala

Predef

object Predef extends LowPriorityImplicits

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.

Source
Predef.scala
Linear Supertypes
LowPriorityImplicits, LowPriorityImplicits2, AnyRef, Any
Ordering
  1. Grouped
  2. Alphabetic
  3. By Inheritance
Inherited
  1. Predef
  2. LowPriorityImplicits
  3. LowPriorityImplicits2
  4. AnyRef
  5. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Utility Methods

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

    NotImplementedError when ??? is invoked.

  2. 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 literal T.class in Java.

    returns

    The runtime Class representation of type T.

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

    A method that returns its input value.

    A method that returns its input value.

    A

    type of the input value x.

    x

    the value of type A to be returned.

    returns

    the value x.

    Annotations
    @inline()
  4. def implicitly[T](implicit e: T): T

    Summon an implicit value of type T.

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

    T

    the type of the value to be summoned

    returns

    the implicit value of type T

    Annotations
    @inline()
  5. 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.

    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.

    Annotations
    @inline()
    Example:
    1. 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
  6. 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.

    Annotations
    @inline()
    Example:
    1. 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.

  1. final def assert(assertion: Boolean, message: => Any): Unit

    Tests an expression, throwing an AssertionError if false.

    Tests an expression, throwing an AssertionError if false. Calls to this method will not be generated if -Xelide-below is greater than ASSERTION.

    assertion

    the expression to test

    message

    a String to include in the failure message

    Annotations
    @elidable(level = ASSERTION) @inline()
    See also

    elidable

  2. def assert(assertion: Boolean): Unit

    Tests an expression, throwing an AssertionError if false.

    Tests an expression, throwing an AssertionError if false. Calls to this method will not be generated if -Xelide-below is greater than ASSERTION.

    assertion

    the expression to test

    Annotations
    @elidable(level = ASSERTION)
    See also

    elidable

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

    assumption

    the expression to test

    message

    a String to include in the failure message

    Annotations
    @elidable(level = ASSERTION) @inline()
    See also

    elidable

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

    assumption

    the expression to test

    Annotations
    @elidable(level = ASSERTION)
    See also

    elidable

  5. 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 to assert, 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()
  6. 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 to assert, 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.

  1. def print(x: Any): Unit

    Prints an object to out using its toString method.

    Prints an object to out using its toString method.

    x

    the object to print; may be null.

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

    xs

    the arguments used to instantiate the pattern.

    Exceptions thrown

    java.lang.IllegalArgumentException if there was a problem with the format string or arguments

    See also

    StringContext.f

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

  4. def println(): Unit

    Prints a newline character on the default output.

Aliases

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

  1. type Class[T] = java.lang.Class[T]

  2. type Function[-A, +B] = (A) => B

  3. type Map[K, +V] = collection.immutable.Map[K, V]

  4. type Set[A] = collection.immutable.Set[A]

  5. type String = java.lang.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. (See the documentation corresponding to your Java version, for example https://docs.oracle.com/javase/8/docs/api/java/lang/String.html.) In addition, extension methods in scala.collection.StringOps are added implicitly through the conversion augmentString.

  1. val ->: Tuple2.type

    Allows destructuring tuples with the same syntax as constructing them.

    Allows destructuring tuples with the same syntax as constructing them.

    Example:
    1. val tup = "foobar" -> 3
      
      val c = tup match {
        case str -> i => str.charAt(i)
      }
  2. val Map: collection.immutable.Map.type

  3. val Set: collection.immutable.Set.type

String Conversions

Conversions from String to StringOps or WrappedString.

  1. implicit def augmentString(x: String): StringOps

    Annotations
    @inline()
  2. implicit def wrapString(s: String): WrappedString

    Definition Classes
    LowPriorityImplicits

Implicit Classes

These implicit classes add useful extension methods to every type.

  1. implicit final class ArrowAssoc[A] extends AnyVal

  2. implicit final class Ensuring[A] extends AnyVal

  3. implicit final class StringFormat[A] extends AnyVal

  4. implicit final class any2stringadd[A] extends AnyVal

    Injects String concatenation operator + to any classes.

    Injects String concatenation operator + to any classes.

    Annotations
    @deprecated
    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.

  1. final class ArrayCharSequence extends CharSequence

  2. final class SeqCharSequence extends CharSequence

  1. def ArrayCharSequence(arrayOfChars: Array[Char]): ArrayCharSequence

  2. def SeqCharSequence(sequenceOfChars: collection.IndexedSeq[Char]): SeqCharSequence

Java to Scala

Implicit conversion from Java primitive wrapper types to Scala equivalents.

  1. implicit def Boolean2boolean(x: java.lang.Boolean): Boolean

  2. implicit def Byte2byte(x: java.lang.Byte): Byte

  3. implicit def Character2char(x: Character): Char

  4. implicit def Double2double(x: java.lang.Double): Double

  5. implicit def Float2float(x: java.lang.Float): Float

  6. implicit def Integer2int(x: Integer): Int

  7. implicit def Long2long(x: java.lang.Long): Long

  8. implicit def Short2short(x: java.lang.Short): Short

Scala to Java

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

  1. implicit def boolean2Boolean(x: Boolean): java.lang.Boolean

  2. implicit def byte2Byte(x: Byte): java.lang.Byte

  3. implicit def char2Character(x: Char): Character

  4. implicit def double2Double(x: Double): java.lang.Double

  5. implicit def float2Float(x: Float): java.lang.Float

  6. implicit def int2Integer(x: Int): Integer

  7. implicit def long2Long(x: Long): java.lang.Long

  8. implicit def short2Short(x: Short): java.lang.Short

Array to ArraySeq

Conversions from Arrays to ArraySeqs.

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

    Definition Classes
    LowPriorityImplicits
  2. implicit def wrapBooleanArray(xs: Array[Boolean]): ofBoolean

    Definition Classes
    LowPriorityImplicits
  3. implicit def wrapByteArray(xs: Array[Byte]): ofByte

    Definition Classes
    LowPriorityImplicits
  4. implicit def wrapCharArray(xs: Array[Char]): ofChar

    Definition Classes
    LowPriorityImplicits
  5. implicit def wrapDoubleArray(xs: Array[Double]): ofDouble

    Definition Classes
    LowPriorityImplicits
  6. implicit def wrapFloatArray(xs: Array[Float]): ofFloat

    Definition Classes
    LowPriorityImplicits
  7. implicit def wrapIntArray(xs: Array[Int]): ofInt

    Definition Classes
    LowPriorityImplicits
  8. implicit def wrapLongArray(xs: Array[Long]): ofLong

    Definition Classes
    LowPriorityImplicits
  9. implicit def wrapRefArray[T <: AnyRef](xs: Array[T]): ofRef[T]

    Definition Classes
    LowPriorityImplicits
  10. implicit def wrapShortArray(xs: Array[Short]): ofShort

    Definition Classes
    LowPriorityImplicits
  11. implicit def wrapUnitArray(xs: Array[Unit]): ofUnit

    Definition Classes
    LowPriorityImplicits

Ungrouped

  1. type Manifest[T] = reflect.Manifest[T]
    Annotations
    @implicitNotFound(msg = "No Manifest available for ${T}.")
  2. type OptManifest[T] = reflect.OptManifest[T]
  1. 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.

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

  2. val Manifest: reflect.Manifest.type
  3. val NoManifest: reflect.NoManifest.type
  4. implicit def booleanArrayOps(xs: Array[Boolean]): ArrayOps[Boolean]
    Annotations
    @inline()
  5. implicit def booleanWrapper(x: Boolean): RichBoolean
    Definition Classes
    LowPriorityImplicits
    Annotations
    @inline()
  6. implicit def byteArrayOps(xs: Array[Byte]): ArrayOps[Byte]
    Annotations
    @inline()
  7. 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()
  8. implicit def charArrayOps(xs: Array[Char]): ArrayOps[Char]
    Annotations
    @inline()
  9. implicit def charWrapper(c: Char): RichChar
    Definition Classes
    LowPriorityImplicits
    Annotations
    @inline()
  10. implicit def doubleArrayOps(xs: Array[Double]): ArrayOps[Double]
    Annotations
    @inline()
  11. implicit def doubleWrapper(x: Double): RichDouble
    Definition Classes
    LowPriorityImplicits
    Annotations
    @inline()
  12. implicit def floatArrayOps(xs: Array[Float]): ArrayOps[Float]
    Annotations
    @inline()
  13. implicit def floatWrapper(x: Float): RichFloat
    Definition Classes
    LowPriorityImplicits
    Annotations
    @inline()
  14. implicit def genericArrayOps[T](xs: Array[T]): ArrayOps[T]
    Annotations
    @inline()
  15. implicit def intArrayOps(xs: Array[Int]): ArrayOps[Int]
    Annotations
    @inline()
  16. implicit def intWrapper(x: Int): RichInt
    Definition Classes
    LowPriorityImplicits
    Annotations
    @inline()
  17. implicit def longArrayOps(xs: Array[Long]): ArrayOps[Long]
    Annotations
    @inline()
  18. implicit def longWrapper(x: Long): RichLong
    Definition Classes
    LowPriorityImplicits
    Annotations
    @inline()
  19. def manifest[T](implicit m: Manifest[T]): Manifest[T]
  20. def optManifest[T](implicit m: OptManifest[T]): OptManifest[T]
  21. implicit def refArrayOps[T <: AnyRef](xs: Array[T]): ArrayOps[T]
    Annotations
    @inline()
  22. implicit def shortArrayOps(xs: Array[Short]): ArrayOps[Short]
    Annotations
    @inline()
  23. implicit def shortWrapper(x: Short): RichShort
    Definition Classes
    LowPriorityImplicits
    Annotations
    @inline()
  24. implicit def tuple2ToZippedOps[T1, T2](x: (T1, T2)): Ops[T1, T2]
  25. implicit def tuple3ToZippedOps[T1, T2, T3](x: (T1, T2, T3)): Ops[T1, T2, T3]
  26. implicit def unitArrayOps(xs: Array[Unit]): ArrayOps[Unit]
    Annotations
    @inline()
  27. implicit def copyArrayToImmutableIndexedSeq[T](xs: Array[T]): IndexedSeq[T]
    Definition Classes
    LowPriorityImplicits2
    Annotations
    @deprecated
    Deprecated

    (Since version 2.13.0) Implicit conversions from Array to immutable.IndexedSeq are implemented by copying; Use the more efficient non-copying ArraySeq.unsafeWrapArray or an explicit toIndexedSeq call