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.
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
- Source
- Predef.scala
- Graph
-
- Supertypes
- Self type
-
Predef.type
Members list
Grouped members
Utility Methods
??? 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. - Source
- Predef.scala
Retrieves the runtime representation of a class type. classOf[T] is equivalent to the class literal T.class in Java.
Retrieves the runtime representation of a class type. classOf[T] is equivalent to the class literal T.class in Java.
Type parameters
- T
-
the type whose runtime class representation is returned
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 - Source
- Predef.scala
A method that returns its input value.
A method that returns its input value.
Type parameters
- A
-
the type of the input value
x
Value parameters
- x
-
the value of type
Ato be returned
Attributes
- Returns
-
the value
x - Source
- Predef.scala
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
Value parameters
- e
-
the implicit value of type
T
Attributes
- Returns
-
the implicit value of type
T - Source
- Predef.scala
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.
Type parameters
- T
-
the type of the expression being guarded
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 - Source
- Predef.scala
Retrieves the single value of a type with a unique inhabitant.
Retrieves the single value of a type with a unique inhabitant.
Type parameters
- T
-
the singleton type whose unique value is retrieved
Value parameters
- vt
-
the implicit
ValueOfinstance that provides the value
Attributes
- Returns
-
the unique inhabitant of type
T - Example
-
object Foo val foo = valueOf[Foo.type] // foo is Foo.type = Foo val bar = valueOf[23] // bar is 23.type = 23 - Source
- Predef.scala
Retrieves the single value of a type with a unique inhabitant.
Retrieves the single value of a type with a unique inhabitant.
Type parameters
- T
-
the singleton type whose unique value is retrieved
Attributes
- Returns
-
the unique inhabitant of type
T - Example
-
object Foo val foo = valueOf[Foo.type] // foo is Foo.type = Foo val bar = valueOf[23] // bar is 23.type = 23 - Source
- Predef.scala
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.
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.
Value parameters
- assumption
-
the expression to test
Attributes
- Source
- Predef.scala
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.
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.
Value parameters
- assumption
-
the expression to test
- message
-
a String to include in the failure message
Attributes
- Source
- Predef.scala
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
- Source
- Predef.scala
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
- Source
- Predef.scala
Console Output
These methods provide output via the console.
Prints an object to out using its toString method.
Prints an object to out using its toString method.
Value parameters
- x
-
the object to print; may be null.
Attributes
- Source
- Predef.scala
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
- Source
- Predef.scala
Prints a newline character on the default output.
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
- Source
- Predef.scala
Aliases
These aliases bring selected immutable types into scope without any imports.
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) } - Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
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
- Source
- Predef.scala
String Conversions
Conversions from String to StringOps or WrappedString.
Value parameters
- s
-
the string to wrap as a
WrappedString
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Implicit Classes
These implicit classes add useful extension methods to every type.
Type parameters
- A
-
the type of the left-hand side of the arrow association
Value parameters
- self
-
the value to use as the first element of the resulting tuple
Attributes
- Source
- Predef.scala
- Supertypes
Type parameters
- A
-
the type of the left-hand side of the arrow association
Value parameters
- self
-
the value to use as the first element of the resulting tuple
Attributes
- Source
- Predef.scala
Type parameters
- A
-
the type of the value being checked with
ensuring
Value parameters
- self
-
the value to check postconditions against
Attributes
- Source
- Predef.scala
- Supertypes
Type parameters
- A
-
the type of the value being checked with
ensuring
Value parameters
- self
-
the value to check postconditions against
Attributes
- Source
- Predef.scala
Type parameters
- A
-
the type of the value to be formatted as a string
Value parameters
- self
-
the value to format
Attributes
- Source
- Predef.scala
- Supertypes
Type parameters
- A
-
the type of the value to be formatted as a string
Value parameters
- self
-
the value to format
Attributes
- Source
- Predef.scala
CharSequence Wrappers
Wrappers that implements CharSequence and were implicit classes.
Value parameters
- arrayOfChars
-
the array of characters to wrap as a
CharSequence
Attributes
- Source
- Predef.scala
- Supertypes
Value parameters
- arrayOfChars
-
the array of characters to wrap as a
CharSequence
Attributes
- Source
- Predef.scala
Value parameters
- sequenceOfChars
-
the indexed sequence of characters to wrap as a
CharSequence
Attributes
- Source
- Predef.scala
- Supertypes
Value parameters
- sequenceOfChars
-
the indexed sequence of characters to wrap as a
CharSequence
Attributes
- Source
- Predef.scala
Java to Scala
Implicit conversion from Java primitive wrapper types to Scala equivalents.
Scala to Java
Implicit conversion from Scala AnyVals to Java primitive wrapper types equivalents.
Array to ArraySeq
Conversions from Arrays to ArraySeqs.
Type parameters
- T
-
the element type of the array
Value parameters
- xs
-
the array to wrap as an
ArraySeq
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Value parameters
- xs
-
the array to wrap as an
ArraySeq
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Value parameters
- xs
-
the array to wrap as an
ArraySeq
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Value parameters
- xs
-
the array to wrap as an
ArraySeq
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Value parameters
- xs
-
the array to wrap as an
ArraySeq
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Value parameters
- xs
-
the array to wrap as an
ArraySeq
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Value parameters
- xs
-
the array to wrap as an
ArraySeq
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Value parameters
- xs
-
the array to wrap as an
ArraySeq
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Type parameters
- T
-
the element type of the array, must be a reference type
Value parameters
- xs
-
the array of reference-typed elements to wrap as an
ArraySeq
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Value parameters
- xs
-
the array to wrap as an
ArraySeq
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Value parameters
- xs
-
the array to wrap as an
ArraySeq
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Type members
Types
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
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
- Source
- Predef.scala
Value members
Concrete methods
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
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
Value parameters
- x
-
the given value of type
T
Attributes
- Returns
-
the given value with its singleton type preserved
- Source
- Predef.scala
Extensions
Extensions
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
- Returns
-
the value cast to its non-nullable type
x.type & T - Example
-
val s1: String | Null = "hello" val s2: String = s1.nn val s3: String | Null = null val s4: String = s3.nn // throw NullPointerException - Source
- Predef.scala
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.
Value parameters
- y
-
the reference to compare against for reference equality
Attributes
- Source
- Predef.scala
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.
Value parameters
- y
-
the reference to compare against for reference non-equality
Attributes
- Source
- Predef.scala
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 warningval xs: List[Int] = List(1,2,3) val y :: ys = xs.runtimeChecked // `_ :: _` can be checked at runtime, so no warning - Source
- Predef.scala
Implicits
Implicits
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 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 A) B).
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Attributes
- Source
- Predef.scala
Inherited implicits
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
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!
Value parameters
- x
-
the primitive value to wrap
Attributes
- Returns
-
a rich wrapper providing additional methods on the primitive value
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
Attributes
- Inherited from:
- LowPriorityImplicits (hidden)
- Source
- Predef.scala
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)
- Source
- Predef.scala