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 http://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.
Test two objects for inequality.
true
if !(this == that), false otherwise.
Equivalent to x.hashCode
except for boxed numeric types and null
. For numerics, it returns a hash value which is consistent with value equality: if two value type instances compare as true, then ## will produce the same hash value for each of them. For null
returns a hashcode where null.hashCode
throws a NullPointerException
.
a hash value consistent with ==
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)
.
Allows destructuring tuples with the same syntax as constructing them.
val tup = "foobar" -> 3 val c = tup match { case str -> i => str.charAt(i) }
The expression x == that
is equivalent to if (x eq null) that eq null else x.equals(that)
.
true
if the receiver object is equivalent to the argument; false
otherwise.
???
can be used for marking methods that remain to be implemented.
NotImplementedError
when ???
is invoked.
Cast the receiver object to be of type T0
.
Note that the success of a cast at runtime is modulo Scala's erasure semantics. Therefore the expression 1.asInstanceOf[String]
will throw a ClassCastException
at runtime, while the expression List(1).asInstanceOf[List[String]]
will not. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the requested type.
the receiver object.
ClassCastException
if the receiver object is not an instance of the erasure of type T0
.
Tests an expression, throwing an AssertionError
if false. Calls to this method will not be generated if -Xelide-below
is greater than ASSERTION
.
the expression to test
a String to include in the failure message
Tests an expression, throwing an AssertionError
if false. Calls to this method will not be generated if -Xelide-below
is greater than ASSERTION
.
the expression to test
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
.
the expression to test
a String to include in the failure message
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
.
the expression to test
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!
Retrieve the runtime representation of a class type. classOf[T]
is equivalent to the class literal T.class
in Java.
The runtime Class representation of type T.
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
Create a copy of the receiver object.
The default implementation of the clone
method is platform dependent.
a copy of the receiver object.
Tests whether the argument (that
) is a reference to the receiver object (this
).
The eq
method implements an equivalence relation on non-null instances of AnyRef
, and has three additional properties:
x
and y
of type AnyRef
, multiple invocations of x.eq(y)
consistently returns true
or consistently returns false
.For any non-null instance x
of type AnyRef
, x.eq(null)
and null.eq(x)
returns false
.
null.eq(null)
returns true
. When overriding the equals
or hashCode
methods, it is important to ensure that their behavior is consistent with reference equality. Therefore, if two objects are references to each other (o1 eq o2
), they should be equal to each other (o1 == o2
) and they should hash to the same value (o1.hashCode == o2.hashCode
).
true
if the argument is a reference to the receiver object; false
otherwise.
The equality method for reference types. Default implementation delegates to eq
.
See also equals
in scala.Any.
true
if the receiver object is equivalent to the argument; false
otherwise.
Called by the garbage collector on the receiver object when there are no more references to the object.
The details of when and if the finalize
method is invoked, as well as the interaction between finalize
and non-local returns and exceptions, are all platform dependent.
Returns the runtime class representation of the object.
a class object corresponding to the runtime type of the receiver.
The hashCode method for reference types. See hashCode in scala.Any.
the hash code value for this object.
A method that returns its input value.
type of the input value x.
the value of type A to be returned.
the value x
.
Summon an implicit value of type T
. Usually, the argument is not passed explicitly.
the type of the value to be summoned
the implicit value of type T
Test whether the dynamic type of the receiver object is T0
.
Note that the result of the test is modulo Scala's erasure semantics. Therefore the expression 1.isInstanceOf[String]
will return false
, while the expression List(1).isInstanceOf[List[String]]
will return true
. In the latter example, because the type argument is erased as part of compilation it is not possible to check whether the contents of the list are of the specified type.
true
if the receiver object is an instance of erasure of type T0
; false
otherwise.
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.
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
Equivalent to !(this eq that)
.
true
if the argument is not a reference to the receiver object; false
otherwise.
Wakes up a single thread that is waiting on the receiver object's monitor.
not specified by SLS as a member of AnyRef
Wakes up all threads that are waiting on the receiver object's monitor.
not specified by SLS as a member of AnyRef
Prints an object to out
using its toString
method.
the object to print; may be null.
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.
the pattern for formatting the arguments.
java.lang.IllegalArgumentException
if there was a problem with the format string or arguments
Prints out an object to the default output, followed by a newline character.
the object to print.
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.
the expression to test
a String to include in the failure message
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.
the expression to test
Creates a String representation of this object. The default representation is platform dependent. On the java platform it is the concatenation of the class name, "@", and the object's hashcode in hexadecimal.
a String representation of the object.
Retrieve the single value of a type with a unique inhabitant.
object Foo val foo = valueOf[Foo.type] // foo is Foo.type = Foo val bar = valueOf[23] // bar is 23.type = 23
© 2002-2019 EPFL, with contributions from Lightbend.
Licensed under the Apache License, Version 2.0.
https://www.scala-lang.org/api/2.13.0/scala/Predef$.html
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 ofassert
can be elided at compile time by providing the command line option-Xdisable-assertions
, which raises-Xelide-below
aboveelidable.ASSERTION
, to thescalac
command.Variants of
assert
intended for use with static analysis tools are also provided:assume
,require
andensuring
.require
andensuring
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,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 fromassert
in that if the condition fails, then the caller of the function is to blame rather than a logical error having been made withinaddNaturals
itself.ensuring
is a form ofassert
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.