AnyVal
is the root class of all value types, which describe values
not implemented as objects in the underlying host system.
AnyVal
is the root class of all value types, which describe values
not implemented as objects in the underlying host system. Value classes
are specified in Scala Language Specification, section 12.2.
The standard implementation includes nine AnyVal
subtypes:
scala.Double, scala.Float, scala.Long, scala.Int, scala.Char, scala.Short, and scala.Byte are the numeric value types.
scala.Unit and scala.Boolean are the non-numeric value types.
Other groupings:
Prior to Scala 2.10, AnyVal
was a sealed trait. Beginning with Scala 2.10,
however, it is possible to define a subclass of AnyVal
called a user-defined value class
which is treated specially by the compiler. Properly-defined user value classes provide a way
to improve performance on user-defined types by avoiding object allocation at runtime, and by
replacing virtual method invocations with static method invocations.
User-defined value classes which avoid object allocation...
val
parameter that is the underlying runtime representation.def
s, but no val
s, var
s, or nested traits
s, class
es or object
s.AnyVal
.equals
or hashCode
methods.A minimal example:
class Wrapper(val underlying: Int) extends AnyVal { def foo: Wrapper = new Wrapper(underlying * 19) }
It's important to note that user-defined value classes are limited, and in some circumstances, still must allocate a value class instance at runtime. These limitations and circumstances are explained in greater detail in the Value Classes and Universal Traits.
The App
trait can be used to quickly turn objects
into executable programs.
The App
trait can be used to quickly turn objects
into executable programs. Here is an example:
object Main extends App { Console.println("Hello World: " + (args mkString ", ")) }
Here, object Main
inherits the main
method of App
.
args
returns the current command line arguments as an array.
It should be noted that this trait is implemented using the DelayedInit functionality, which means that fields of the object will not have been initialized before the main method has been executed.
It should also be noted that the main
method should not be overridden:
the whole class body becomes the "main method".
Future versions of this trait will no longer extend DelayedInit
.
2.1, 15/02/2011
Arrays are mutable, indexed collections of values.
Arrays are mutable, indexed collections of values. Array[T]
is Scala's representation
for Java's T[]
.
val numbers = Array(1, 2, 3, 4) val first = numbers(0) // read the first element numbers(3) = 100 // replace the 4th array element with 100 val biggerNumbers = numbers.map(_ * 2) // multiply all numbers by two
Arrays make use of two common pieces of Scala syntactic sugar, shown on lines 2 and 3 of the above
example code.
Line 2 is translated into a call to apply(Int)
, while line 3 is translated into a call to
update(Int, T)
.
Two implicit conversions exist in scala.Predef that are frequently applied to arrays: a conversion
to scala.collection.mutable.ArrayOps (shown on line 4 of the example above) and a conversion
to scala.collection.mutable.WrappedArray (a subtype of scala.collection.Seq).
Both types make available many of the standard operations found in the Scala collections API.
The conversion to ArrayOps
is temporary, as all operations defined on ArrayOps
return an Array
,
while the conversion to WrappedArray
is permanent as all operations return a WrappedArray
.
The conversion to ArrayOps
takes priority over the conversion to WrappedArray
. For instance,
consider the following code:
val arr = Array(1, 2, 3) val arrReversed = arr.reverse val seqReversed : Seq[Int] = arr.reverse
Value arrReversed
will be of type Array[Int]
, with an implicit conversion to ArrayOps
occurring
to perform the reverse
operation. The value of seqReversed
, on the other hand, will be computed
by converting to WrappedArray
first and invoking the variant of reverse
that returns another
WrappedArray
.
1.0
"The Scala 2.8 Collections' API" section on Array
by Martin Odersky for more information.
"Scala 2.8 Arrays" the Scala Improvement Document detailing arrays since Scala 2.8.
Scala Language Specification, for in-depth information on the transformations the Scala compiler makes on Arrays (Sections 6.6 and 6.15 respectively.)
Boolean
(equivalent to Java's boolean
primitive type) is a
subtype of scala.AnyVal.
Boolean
(equivalent to Java's boolean
primitive type) is a
subtype of scala.AnyVal. Instances of Boolean
are not
represented by an object in the underlying runtime system.
There is an implicit conversion from scala.Boolean => scala.runtime.RichBoolean which provides useful non-primitive operations.
Byte
, a 8-bit signed integer (equivalent to Java's byte
primitive type) is a
subtype of scala.AnyVal.
Byte
, a 8-bit signed integer (equivalent to Java's byte
primitive type) is a
subtype of scala.AnyVal. Instances of Byte
are not
represented by an object in the underlying runtime system.
There is an implicit conversion from scala.Byte => scala.runtime.RichByte which provides useful non-primitive operations.
Char
, a 16-bit unsigned integer (equivalent to Java's char
primitive type) is a
subtype of scala.AnyVal.
Char
, a 16-bit unsigned integer (equivalent to Java's char
primitive type) is a
subtype of scala.AnyVal. Instances of Char
are not
represented by an object in the underlying runtime system.
There is an implicit conversion from scala.Char => scala.runtime.RichChar which provides useful non-primitive operations.
Classes extending this trait are cloneable across platforms (Java, .NET).
Double
, a 64-bit IEEE-754 floating point number (equivalent to Java's double
primitive type) is a
subtype of scala.AnyVal.
Double
, a 64-bit IEEE-754 floating point number (equivalent to Java's double
primitive type) is a
subtype of scala.AnyVal. Instances of Double
are not
represented by an object in the underlying runtime system.
There is an implicit conversion from scala.Double => scala.runtime.RichDouble which provides useful non-primitive operations.
A marker trait that enables dynamic invocations.
A marker trait that enables dynamic invocations. Instances x
of this
trait allow method invocations x.meth(args)
for arbitrary method
names meth
and argument lists args
as well as field accesses
x.field
for arbitrary field names field
.
If a call is not natively supported by x
(i.e. if type checking
fails), it is rewritten according to the following rules:
foo.method("blah") ~~> foo.applyDynamic("method")("blah") foo.method(x = "blah") ~~> foo.applyDynamicNamed("method")(("x", "blah")) foo.method(x = 1, 2) ~~> foo.applyDynamicNamed("method")(("x", 1), ("", 2)) foo.field ~~> foo.selectDynamic("field") foo.varia = 10 ~~> foo.updateDynamic("varia")(10) foo.arr(10) = 13 ~~> foo.selectDynamic("arr").update(10, 13) foo.arr(10) ~~> foo.applyDynamic("arr")(10)
As of Scala 2.10, defining direct or indirect subclasses of this trait
is only possible if the language feature dynamics
is enabled.
Defines a finite set of values specific to the enumeration.
Defines a finite set of values specific to the enumeration. Typically these values enumerate all possible forms something can take and provide a lightweight alternative to case classes.
Each call to a Value
method adds a new unique value to the enumeration.
To be accessible, these values are usually defined as val
members of
the evaluation.
All values in an enumeration share a common, unique type defined as the
Value
type member of the enumeration (Value
selected on the stable
identifier path of the enumeration instance).
object Main extends App { object WeekDay extends Enumeration { type WeekDay = Value val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value } import WeekDay._ def isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun) WeekDay.values filter isWorkingDay foreach println } // output: // Mon // Tue // Wed // Thu // Fri
An interface containing operations for equality.
An interface containing operations for equality.
The only method not already present in class AnyRef
is canEqual
.
Contains a fallback builder for arrays when the element type does not have a class tag.
Contains a fallback builder for arrays when the element type does not have a class tag. In that case a generic array is built.
Float
, a 32-bit IEEE-754 floating point number (equivalent to Java's float
primitive type) is a
subtype of scala.AnyVal.
Float
, a 32-bit IEEE-754 floating point number (equivalent to Java's float
primitive type) is a
subtype of scala.AnyVal. Instances of Float
are not
represented by an object in the underlying runtime system.
There is an implicit conversion from scala.Float => scala.runtime.RichFloat which provides useful non-primitive operations.
A function of 0 parameters.
A function of 0 parameters.
In the following example, the definition of javaVersion is a shorthand for the anonymous class definition anonfun0:
object Main extends App { val javaVersion = () => sys.props("java.version") val anonfun0 = new Function0[String] { def apply(): String = sys.props("java.version") } assert(javaVersion() == anonfun0()) }
A function of 1 parameter.
A function of 1 parameter.
In the following example, the definition of succ is a shorthand for the anonymous class definition anonfun1:
object Main extends App { val succ = (x: Int) => x + 1 val anonfun1 = new Function1[Int, Int] { def apply(x: Int): Int = x + 1 } assert(succ(0) == anonfun1(0)) }
Note that the difference between Function1
and scala.PartialFunction
is that the latter can specify inputs which it will not handle.
A function of 10 parameters.
A function of 11 parameters.
A function of 12 parameters.
A function of 13 parameters.
A function of 14 parameters.
A function of 15 parameters.
A function of 16 parameters.
A function of 17 parameters.
A function of 18 parameters.
A function of 19 parameters.
A function of 2 parameters.
A function of 2 parameters.
In the following example, the definition of max is a shorthand for the anonymous class definition anonfun2:
object Main extends App { val max = (x: Int, y: Int) => if (x < y) y else x val anonfun2 = new Function2[Int, Int, Int] { def apply(x: Int, y: Int): Int = if (x < y) y else x } assert(max(0, 1) == anonfun2(0, 1)) }
A function of 20 parameters.
A function of 21 parameters.
A function of 22 parameters.
A function of 3 parameters.
A function of 4 parameters.
A function of 5 parameters.
A function of 6 parameters.
A function of 7 parameters.
A function of 8 parameters.
A function of 9 parameters.
A marker trait for all immutable datastructures such as immutable collections.
A marker trait for all immutable datastructures such as immutable collections.
2.8
Int
, a 32-bit signed integer (equivalent to Java's int
primitive type) is a
subtype of scala.AnyVal.
Int
, a 32-bit signed integer (equivalent to Java's int
primitive type) is a
subtype of scala.AnyVal. Instances of Int
are not
represented by an object in the underlying runtime system.
There is an implicit conversion from scala.Int => scala.runtime.RichInt which provides useful non-primitive operations.
Long
, a 64-bit signed integer (equivalent to Java's long
primitive type) is a
subtype of scala.AnyVal.
Long
, a 64-bit signed integer (equivalent to Java's long
primitive type) is a
subtype of scala.AnyVal. Instances of Long
are not
represented by an object in the underlying runtime system.
There is an implicit conversion from scala.Long => scala.runtime.RichLong which provides useful non-primitive operations.
This class implements errors which are thrown whenever an object doesn't match any pattern of a pattern matching expression.
This class implements errors which are thrown whenever an object doesn't match any pattern of a pattern matching expression.
1.1, 05/03/2004
2.0
A marker trait for mutable data structures such as mutable collections
A marker trait for mutable data structures such as mutable collections
2.8
Throwing this exception can be a temporary replacement for a method body that remains to be implemented.
Throwing this exception can be a temporary replacement for a method
body that remains to be implemented. For instance, the exception is thrown by
Predef.???
.
Represents optional values.
Represents optional values. Instances of Option
are either an instance of scala.Some or the object None
.
The most idiomatic way to use an scala.Option instance is to treat it
as a collection or monad and use map
,flatMap
, filter
, or
foreach
:
val name: Option[String] = request getParameter "name" val upper = name map { _.trim } filter { _.length != 0 } map { _.toUpperCase } println(upper getOrElse "")
Note that this is equivalent to
val upper = for { name <- request getParameter "name" trimmed <- Some(name.trim) upper <- Some(trimmed.toUpperCase) if trimmed.length != 0 } yield upper println(upper getOrElse "")
Because of how for comprehension works, if None
is returned
from request.getParameter
, the entire expression results in
None
This allows for sophisticated chaining of scala.Option values without having to check for the existence of a value.
A less-idiomatic way to use scala.Option values is via pattern matching:
val nameMaybe = request getParameter "name" nameMaybe match { case Some(name) => println(name.trim.toUppercase) case None => println("No name value") }
1.1, 16/01/2007
Many of the methods in here are duplicative with those in the Traversable hierarchy, but they are duplicated for a reason: the implicit conversion tends to leave one with an Iterable in situations where one could have retained an Option.
A partial function of type PartialFunction[A, B]
is a unary function
where the domain does not necessarily include all values of type A
.
A partial function of type PartialFunction[A, B]
is a unary function
where the domain does not necessarily include all values of type A
.
The function isDefinedAt
allows to test dynamically if a value is in
the domain of the function.
Even if isDefinedAt
returns true for an a: A
, calling apply(a)
may
still throw an exception, so the following code is legal:
val f: PartialFunction[Int, Any] = { case _ => 1/0 }
It is the responsibility of the caller to call isDefinedAt
before
calling apply
, because if isDefinedAt
is false, it is not guaranteed
apply
will throw an exception to indicate an error condition. If an
exception is not thrown, evaluation may result in an arbitrary value.
The main distinction between PartialFunction
and scala.Function1 is
that the user of a PartialFunction
may choose to do something different
with input that is declared to be outside its domain. For example:
val sample = 1 to 10 val isEven: PartialFunction[Int, String] = { case x if x % 2 == 0 => x+" is even" } // the method collect can use isDefinedAt to select which members to collect val evenNumbers = sample collect isEven val isOdd: PartialFunction[Int, String] = { case x if x % 2 == 1 => x+" is odd" } // the method orElse allows chaining another partial function to handle // input outside the declared domain val numbers = sample map (isEven orElse isOdd)
1.0, 16/07/2003
Base trait for all products, which in the standard library include at least scala.Product1 through scala.Product22 and therefore also their subclasses scala.Tuple1 through scala.Tuple22.
Base trait for all products, which in the standard library include at
least scala.Product1 through scala.Product22 and therefore also
their subclasses scala.Tuple1 through scala.Tuple22. In addition,
all case classes implement Product
with synthetically generated methods.
1.0
2.3
Product1 is a cartesian product of 1 component.
Product1 is a cartesian product of 1 component.
2.3
Product10 is a cartesian product of 10 components.
Product10 is a cartesian product of 10 components.
2.3
Product11 is a cartesian product of 11 components.
Product11 is a cartesian product of 11 components.
2.3
Product12 is a cartesian product of 12 components.
Product12 is a cartesian product of 12 components.
2.3
Product13 is a cartesian product of 13 components.
Product13 is a cartesian product of 13 components.
2.3
Product14 is a cartesian product of 14 components.
Product14 is a cartesian product of 14 components.
2.3
Product15 is a cartesian product of 15 components.
Product15 is a cartesian product of 15 components.
2.3
Product16 is a cartesian product of 16 components.
Product16 is a cartesian product of 16 components.
2.3
Product17 is a cartesian product of 17 components.
Product17 is a cartesian product of 17 components.
2.3
Product18 is a cartesian product of 18 components.
Product18 is a cartesian product of 18 components.
2.3
Product19 is a cartesian product of 19 components.
Product19 is a cartesian product of 19 components.
2.3
Product2 is a cartesian product of 2 components.
Product2 is a cartesian product of 2 components.
2.3
Product20 is a cartesian product of 20 components.
Product20 is a cartesian product of 20 components.
2.3
Product21 is a cartesian product of 21 components.
Product21 is a cartesian product of 21 components.
2.3
Product22 is a cartesian product of 22 components.
Product22 is a cartesian product of 22 components.
2.3
Product3 is a cartesian product of 3 components.
Product3 is a cartesian product of 3 components.
2.3
Product4 is a cartesian product of 4 components.
Product4 is a cartesian product of 4 components.
2.3
Product5 is a cartesian product of 5 components.
Product5 is a cartesian product of 5 components.
2.3
Product6 is a cartesian product of 6 components.
Product6 is a cartesian product of 6 components.
2.3
Product7 is a cartesian product of 7 components.
Product7 is a cartesian product of 7 components.
2.3
Product8 is a cartesian product of 8 components.
Product8 is a cartesian product of 8 components.
2.3
Product9 is a cartesian product of 9 components.
Product9 is a cartesian product of 9 components.
2.3
This class implements a simple proxy that forwards all calls to
the public, non-final methods defined in class Any
to another
object self.
This class implements a simple proxy that forwards all calls to
the public, non-final methods defined in class Any
to another
object self. Those methods are:
def hashCode(): Int def equals(other: Any): Boolean def toString(): String
Note: forwarding methods in this way will most likely create an asymmetric equals method, which is not generally recommended.
1.0, 26/04/2004
An exception that indicates an error during Scala reflection
Annotation for specifying the static SerialVersionUID
field
of a serializable class.
Classes extending this trait are serializable across platforms (Java, .NET).
Short
, a 16-bit signed integer (equivalent to Java's short
primitive type) is a
subtype of scala.AnyVal.
Short
, a 16-bit signed integer (equivalent to Java's short
primitive type) is a
subtype of scala.AnyVal. Instances of Short
are not
represented by an object in the underlying runtime system.
There is an implicit conversion from scala.Short => scala.runtime.RichShort which provides useful non-primitive operations.
Class Some[A]
represents existing values of type
A
.
Class Some[A]
represents existing values of type
A
.
1.0, 16/07/2003
A common supertype for companions of specializable types.
A common supertype for companions of specializable types. Should not be extended in user code.
This class provides the basic mechanism to do String Interpolation.
This class provides the basic mechanism to do String Interpolation. String Interpolation allows users to embed variable references directly in *processed* string literals. Here's an example:
val name = "James" println(s"Hello, $name") // Hello, James
Any processed string literal is rewritten as an instantiation and method call against this class. For example:
s"Hello, $name"
is rewritten to be:
StringContext("Hello, ", "").s(name)
By default, this class provides the raw
, s
and f
methods as
available interpolators.
To provide your own string interpolator, create an implicit class
which adds a method to StringContext
. Here's an example:
implicit class JsonHelper(private val sc: StringContext) extends AnyVal { def json(args: Any*): JSONObject = ... } val x: JSONObject = json"{ a: $a }"
Here the JsonHelper
extension class implicitly adds the json
method to
StringContext
which can be used for json
string literals.
The parts that make up the interpolated string, without the expressions that get inserted by interpolation.
2.10.0
A tuple of 1 elements; the canonical representation of a scala.Product1.
A tuple of 1 elements; the canonical representation of a scala.Product1.
Element 1 of this Tuple1
A tuple of 10 elements; the canonical representation of a scala.Product10.
A tuple of 10 elements; the canonical representation of a scala.Product10.
Element 1 of this Tuple10
Element 2 of this Tuple10
Element 3 of this Tuple10
Element 4 of this Tuple10
Element 5 of this Tuple10
Element 6 of this Tuple10
Element 7 of this Tuple10
Element 8 of this Tuple10
Element 9 of this Tuple10
Element 10 of this Tuple10
A tuple of 11 elements; the canonical representation of a scala.Product11.
A tuple of 11 elements; the canonical representation of a scala.Product11.
Element 1 of this Tuple11
Element 2 of this Tuple11
Element 3 of this Tuple11
Element 4 of this Tuple11
Element 5 of this Tuple11
Element 6 of this Tuple11
Element 7 of this Tuple11
Element 8 of this Tuple11
Element 9 of this Tuple11
Element 10 of this Tuple11
Element 11 of this Tuple11
A tuple of 12 elements; the canonical representation of a scala.Product12.
A tuple of 12 elements; the canonical representation of a scala.Product12.
Element 1 of this Tuple12
Element 2 of this Tuple12
Element 3 of this Tuple12
Element 4 of this Tuple12
Element 5 of this Tuple12
Element 6 of this Tuple12
Element 7 of this Tuple12
Element 8 of this Tuple12
Element 9 of this Tuple12
Element 10 of this Tuple12
Element 11 of this Tuple12
Element 12 of this Tuple12
A tuple of 13 elements; the canonical representation of a scala.Product13.
A tuple of 13 elements; the canonical representation of a scala.Product13.
Element 1 of this Tuple13
Element 2 of this Tuple13
Element 3 of this Tuple13
Element 4 of this Tuple13
Element 5 of this Tuple13
Element 6 of this Tuple13
Element 7 of this Tuple13
Element 8 of this Tuple13
Element 9 of this Tuple13
Element 10 of this Tuple13
Element 11 of this Tuple13
Element 12 of this Tuple13
Element 13 of this Tuple13
A tuple of 14 elements; the canonical representation of a scala.Product14.
A tuple of 14 elements; the canonical representation of a scala.Product14.
Element 1 of this Tuple14
Element 2 of this Tuple14
Element 3 of this Tuple14
Element 4 of this Tuple14
Element 5 of this Tuple14
Element 6 of this Tuple14
Element 7 of this Tuple14
Element 8 of this Tuple14
Element 9 of this Tuple14
Element 10 of this Tuple14
Element 11 of this Tuple14
Element 12 of this Tuple14
Element 13 of this Tuple14
Element 14 of this Tuple14
A tuple of 15 elements; the canonical representation of a scala.Product15.
A tuple of 15 elements; the canonical representation of a scala.Product15.
Element 1 of this Tuple15
Element 2 of this Tuple15
Element 3 of this Tuple15
Element 4 of this Tuple15
Element 5 of this Tuple15
Element 6 of this Tuple15
Element 7 of this Tuple15
Element 8 of this Tuple15
Element 9 of this Tuple15
Element 10 of this Tuple15
Element 11 of this Tuple15
Element 12 of this Tuple15
Element 13 of this Tuple15
Element 14 of this Tuple15
Element 15 of this Tuple15
A tuple of 16 elements; the canonical representation of a scala.Product16.
A tuple of 16 elements; the canonical representation of a scala.Product16.
Element 1 of this Tuple16
Element 2 of this Tuple16
Element 3 of this Tuple16
Element 4 of this Tuple16
Element 5 of this Tuple16
Element 6 of this Tuple16
Element 7 of this Tuple16
Element 8 of this Tuple16
Element 9 of this Tuple16
Element 10 of this Tuple16
Element 11 of this Tuple16
Element 12 of this Tuple16
Element 13 of this Tuple16
Element 14 of this Tuple16
Element 15 of this Tuple16
Element 16 of this Tuple16
A tuple of 17 elements; the canonical representation of a scala.Product17.
A tuple of 17 elements; the canonical representation of a scala.Product17.
Element 1 of this Tuple17
Element 2 of this Tuple17
Element 3 of this Tuple17
Element 4 of this Tuple17
Element 5 of this Tuple17
Element 6 of this Tuple17
Element 7 of this Tuple17
Element 8 of this Tuple17
Element 9 of this Tuple17
Element 10 of this Tuple17
Element 11 of this Tuple17
Element 12 of this Tuple17
Element 13 of this Tuple17
Element 14 of this Tuple17
Element 15 of this Tuple17
Element 16 of this Tuple17
Element 17 of this Tuple17
A tuple of 18 elements; the canonical representation of a scala.Product18.
A tuple of 18 elements; the canonical representation of a scala.Product18.
Element 1 of this Tuple18
Element 2 of this Tuple18
Element 3 of this Tuple18
Element 4 of this Tuple18
Element 5 of this Tuple18
Element 6 of this Tuple18
Element 7 of this Tuple18
Element 8 of this Tuple18
Element 9 of this Tuple18
Element 10 of this Tuple18
Element 11 of this Tuple18
Element 12 of this Tuple18
Element 13 of this Tuple18
Element 14 of this Tuple18
Element 15 of this Tuple18
Element 16 of this Tuple18
Element 17 of this Tuple18
Element 18 of this Tuple18
A tuple of 19 elements; the canonical representation of a scala.Product19.
A tuple of 19 elements; the canonical representation of a scala.Product19.
Element 1 of this Tuple19
Element 2 of this Tuple19
Element 3 of this Tuple19
Element 4 of this Tuple19
Element 5 of this Tuple19
Element 6 of this Tuple19
Element 7 of this Tuple19
Element 8 of this Tuple19
Element 9 of this Tuple19
Element 10 of this Tuple19
Element 11 of this Tuple19
Element 12 of this Tuple19
Element 13 of this Tuple19
Element 14 of this Tuple19
Element 15 of this Tuple19
Element 16 of this Tuple19
Element 17 of this Tuple19
Element 18 of this Tuple19
Element 19 of this Tuple19
A tuple of 2 elements; the canonical representation of a scala.Product2.
A tuple of 2 elements; the canonical representation of a scala.Product2.
Element 1 of this Tuple2
Element 2 of this Tuple2
A tuple of 20 elements; the canonical representation of a scala.Product20.
A tuple of 20 elements; the canonical representation of a scala.Product20.
Element 1 of this Tuple20
Element 2 of this Tuple20
Element 3 of this Tuple20
Element 4 of this Tuple20
Element 5 of this Tuple20
Element 6 of this Tuple20
Element 7 of this Tuple20
Element 8 of this Tuple20
Element 9 of this Tuple20
Element 10 of this Tuple20
Element 11 of this Tuple20
Element 12 of this Tuple20
Element 13 of this Tuple20
Element 14 of this Tuple20
Element 15 of this Tuple20
Element 16 of this Tuple20
Element 17 of this Tuple20
Element 18 of this Tuple20
Element 19 of this Tuple20
Element 20 of this Tuple20
A tuple of 21 elements; the canonical representation of a scala.Product21.
A tuple of 21 elements; the canonical representation of a scala.Product21.
Element 1 of this Tuple21
Element 2 of this Tuple21
Element 3 of this Tuple21
Element 4 of this Tuple21
Element 5 of this Tuple21
Element 6 of this Tuple21
Element 7 of this Tuple21
Element 8 of this Tuple21
Element 9 of this Tuple21
Element 10 of this Tuple21
Element 11 of this Tuple21
Element 12 of this Tuple21
Element 13 of this Tuple21
Element 14 of this Tuple21
Element 15 of this Tuple21
Element 16 of this Tuple21
Element 17 of this Tuple21
Element 18 of this Tuple21
Element 19 of this Tuple21
Element 20 of this Tuple21
Element 21 of this Tuple21
A tuple of 22 elements; the canonical representation of a scala.Product22.
A tuple of 22 elements; the canonical representation of a scala.Product22.
Element 1 of this Tuple22
Element 2 of this Tuple22
Element 3 of this Tuple22
Element 4 of this Tuple22
Element 5 of this Tuple22
Element 6 of this Tuple22
Element 7 of this Tuple22
Element 8 of this Tuple22
Element 9 of this Tuple22
Element 10 of this Tuple22
Element 11 of this Tuple22
Element 12 of this Tuple22
Element 13 of this Tuple22
Element 14 of this Tuple22
Element 15 of this Tuple22
Element 16 of this Tuple22
Element 17 of this Tuple22
Element 18 of this Tuple22
Element 19 of this Tuple22
Element 20 of this Tuple22
Element 21 of this Tuple22
Element 22 of this Tuple22
A tuple of 3 elements; the canonical representation of a scala.Product3.
A tuple of 3 elements; the canonical representation of a scala.Product3.
Element 1 of this Tuple3
Element 2 of this Tuple3
Element 3 of this Tuple3
A tuple of 4 elements; the canonical representation of a scala.Product4.
A tuple of 4 elements; the canonical representation of a scala.Product4.
Element 1 of this Tuple4
Element 2 of this Tuple4
Element 3 of this Tuple4
Element 4 of this Tuple4
A tuple of 5 elements; the canonical representation of a scala.Product5.
A tuple of 5 elements; the canonical representation of a scala.Product5.
Element 1 of this Tuple5
Element 2 of this Tuple5
Element 3 of this Tuple5
Element 4 of this Tuple5
Element 5 of this Tuple5
A tuple of 6 elements; the canonical representation of a scala.Product6.
A tuple of 6 elements; the canonical representation of a scala.Product6.
Element 1 of this Tuple6
Element 2 of this Tuple6
Element 3 of this Tuple6
Element 4 of this Tuple6
Element 5 of this Tuple6
Element 6 of this Tuple6
A tuple of 7 elements; the canonical representation of a scala.Product7.
A tuple of 7 elements; the canonical representation of a scala.Product7.
Element 1 of this Tuple7
Element 2 of this Tuple7
Element 3 of this Tuple7
Element 4 of this Tuple7
Element 5 of this Tuple7
Element 6 of this Tuple7
Element 7 of this Tuple7
A tuple of 8 elements; the canonical representation of a scala.Product8.
A tuple of 8 elements; the canonical representation of a scala.Product8.
Element 1 of this Tuple8
Element 2 of this Tuple8
Element 3 of this Tuple8
Element 4 of this Tuple8
Element 5 of this Tuple8
Element 6 of this Tuple8
Element 7 of this Tuple8
Element 8 of this Tuple8
A tuple of 9 elements; the canonical representation of a scala.Product9.
A tuple of 9 elements; the canonical representation of a scala.Product9.
Element 1 of this Tuple9
Element 2 of this Tuple9
Element 3 of this Tuple9
Element 4 of this Tuple9
Element 5 of this Tuple9
Element 6 of this Tuple9
Element 7 of this Tuple9
Element 8 of this Tuple9
Element 9 of this Tuple9
This class represents uninitialized variable/value errors.
This class represents uninitialized variable/value errors.
2.5
This class implements errors which are thrown whenever a field is used before it has been initialized.
This class implements errors which are thrown whenever a field is used before it has been initialized.
Such runtime checks are not emitted by default.
They can be enabled by the -Xcheckinit
compiler option.
2.7
Unit
is a subtype of scala.AnyVal.
Unit
is a subtype of scala.AnyVal. There is only one value of type
Unit
, ()
, and it is not represented by any object in the underlying
runtime system. A method with return type Unit
is analogous to a Java
method which is declared void
.
An annotation that designates that a definition is deprecated.
An annotation that designates that a definition is deprecated. Access to the member then generates a deprecated warning.
2.3
An annotation that designates the name of the parameter to which it is applied as deprecated.
An annotation that designates the name of the parameter to which it is applied as deprecated. Using that name in a named argument generates a deprecation warning.
For instance, evaluating the code below in the Scala interpreter
def inc(x: Int, @deprecatedName('y) n: Int): Int = x + n inc(1, y = 2)
will produce the following output:
warning: there were 1 deprecation warnings; re-run with -deprecation for details res0: Int = 3
2.8.1
An annotation on methods that requests that the compiler should try especially hard to inline the annotated method.
An annotation on methods that requests that the compiler should try especially hard to inline the annotated method.
1.0, 2007-5-21
Marker for native methods.
Marker for native methods.
@native def f(x: Int, y: List[Long]): String = ...
Method body is not generated if method is marked with @native
,
but it is type checked when present.
2.6
An annotation on methods that forbids the compiler to inline the method, no matter how safe the inlining appears to be.
An annotation on methods that forbids the compiler to inline the method, no matter how safe the inlining appears to be.
1.0, 2007-5-21
2.5
An annotation that designates the class to which it is applied as remotable.
An annotation that designates the class to which it is applied as remotable.
For instance, the Scala code
@remote trait Hello { def sayHello(): String }
is equivalent to the following Java code:
public interface Hello extends java.rmi.Remote { String sayHello() throws java.rmi.RemoteException; }
Annotate type parameters on which code should be automatically specialized.
Annotate type parameters on which code should be automatically specialized. For example:
class MyList[@specialized T] ...
Type T can be specialized on a subset of the primitive types by specifying a list of primitive types to specialize at:
class MyList[@specialized(Int, Double, Boolean) T] ..
2.8
Annotation for specifying the exceptions thrown by a method.
Annotation for specifying the exceptions thrown by a method. For example:
class Reader(fname: String) { private val in = new BufferedReader(new FileReader(fname)) @throws[IOException]("if the file doesn't exist") def read() = in.read() }
1.0, 19/05/2006
2.1
An annotation to designate that the annotated entity should not be considered for additional compiler checks.
An annotation to designate that the annotated entity should not be considered for additional compiler checks. Specific applications include annotating the subject of a match expression to suppress exhaustiveness warnings, and annotating a type argument in a match case to suppress unchecked warnings.
Such suppression should be used with caution, without which one may encounter scala.MatchError or java.lang.ClassCastException at runtime. In most cases one can and should address the warning instead of suppressing it.
object Test extends App { // This would normally warn "match is not exhaustive" // because `None` is not covered. def f(x: Option[String]) = (x: @unchecked) match { case Some(y) => y } // This would normally warn "type pattern is unchecked" // but here will blindly cast the head element to String. def g(xs: Any) = xs match { case x: List[String @unchecked] => x.head } }
2.4
Classes and objects (but note, not traits) inheriting the DelayedInit
marker trait will have their initialization code rewritten as follows:
code
becomes delayedInit(code)
.
Classes and objects (but note, not traits) inheriting the DelayedInit
marker trait will have their initialization code rewritten as follows:
code
becomes delayedInit(code)
.
Initialization code comprises all statements and all value definitions that are executed during initialization.
Example:
trait Helper extends DelayedInit { def delayedInit(body: => Unit) = { println("dummy text, printed before initialization of C") body // evaluates the initialization code of C } } class C extends Helper { println("this is the initialization code of C") } object Test extends App { val c = new C }
Should result in the following being printed:
dummy text, printed before initialization of C
this is the initialization code of C
(Since version 2.11.0) DelayedInit semantics can be surprising. Support for App
will continue.
See the release notes for more details: https://github.com/scala/scala/releases/tag/v2.11.0-RC1
"Delayed Initialization" subsection of the Scala Language Specification (section 5.1)
A marker trait for things that are not allowed to be null
A marker trait for things that are not allowed to be null
(Since version 2.11.0) This trait will be removed
2.5
Instances of responder are the building blocks of small programs written in continuation passing style.
Instances of responder are the building blocks of small programs written in continuation passing style. By using responder classes in for comprehensions, one can embed domain-specific languages in Scala while giving the impression that programs in these DSLs are written in direct style.
(Since version 2.11.0) This class will be removed
1.0
2.1
Utility methods for operating on arrays.
Utility methods for operating on arrays. For example:
val a = Array(1, 2) val b = Array.ofDim[Int](2) val c = Array.concat(a, b)
where the array objects a
, b
and c
have respectively the values
Array(1, 2)
, Array(0, 0)
and Array(1, 2, 0, 0)
.
1.0
Implements functionality for printing Scala values on the terminal as well as reading specific values.
Implements functionality for printing Scala values on the terminal as well as reading specific values. Also defines constants for marking up text on ANSI terminals.
1.0, 03/09/2003
A module defining utility methods for higher-order functional programming.
A module defining utility methods for higher-order functional programming.
1.0, 29/11/2006
This case object represents non-existent values.
This case object represents non-existent values.
1.0, 16/07/2003
A few handy operations which leverage the extra bit of information available in partial functions.
A few handy operations which leverage the extra bit of information available in partial functions. Examples:
import PartialFunction._ def strangeConditional(other: Any): Boolean = cond(other) { case x: String if x == "abc" || x == "def" => true case x: Int => true } def onlyInt(v: Any): Option[Int] = condOpt(v) { case x: Int => x }
2.8
The Predef
object provides definitions that are accessible in all Scala
compilation units without explicit qualification.
The Predef
object provides definitions that are accessible in all Scala
compilation units without explicit qualification.
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).
Predef provides a number of simple functions for console I/O, such as
print
, println
, readLine
, readInt
, etc. These functions are all
aliases of the functions provided by scala.Console.
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.
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.
Contains the base traits and objects needed to use and extend Scala's collection library.
Contains the base traits and objects needed to use and extend Scala's collection library.
A detailed guide for using the collections library is available at http://docs.scala-lang.org/overviews/collections/introduction.html. Developers looking to extend the collections library can find a description of its architecture at http://docs.scala-lang.org/overviews/core/architecture-of-scala-collections.html.
It is convenient to treat all collections as either a scala.collection.Traversable or scala.collection.Iterable, as these traits define the vast majority of operations on a collection.
Collections can, of course, be treated as specifically as needed, and the library is designed to ensure that the methods that transform collections will return a collection of the same type:
scala> val array = Array(1,2,3,4,5,6) array: Array[Int] = Array(1, 2, 3, 4, 5, 6) scala> array map { _.toString } res0: Array[String] = Array(1, 2, 3, 4, 5, 6) scala> val list = List(1,2,3,4,5,6) list: List[Int] = List(1, 2, 3, 4, 5, 6) scala> list map { _.toString } res1: List[String] = List(1, 2, 3, 4, 5, 6)
The most common way to create a collection is to use its companion object as
a factory. The three most commonly used collections are
scala.collection.Seq, scala.collection.immutable.Set, and
scala.collection.immutable.Map.
They can be used directly as shown below since their companion objects are
all available as type aliases in either the scala package or in
scala.Predef
. New collections are created like this:
scala> val seq = Seq(1,2,3,4,1) seq: Seq[Int] = List(1, 2, 3, 4, 1) scala> val set = Set(1,2,3,4,1) set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4) scala> val map = Map(1 -> "one", 2 -> "two", 3 -> "three", 2 -> "too") map: scala.collection.immutable.Map[Int,String] = Map(1 -> one, 2 -> too, 3 -> three)
It is also typical to prefer the scala.collection.immutable collections
over those in scala.collection.mutable; the types aliased in
the scala.Predef
object are the immutable versions.
Also note that the collections library was carefully designed to include several implementations of each of the three basic collection types. These implementations have specific performance characteristics which are described in the guide.
The concrete parallel collections also have specific performance characteristics which are described in the parallel collections guide
The scala.collection.JavaConversions object provides implicit defs that will allow mostly seamless integration between APIs using Java Collections and the Scala collections library.
Alternatively the scala.collection.JavaConverters object provides a collection
of decorators that allow converting between Scala and Java collections using asScala
and asJava
methods.
This package object contains primitives for concurrent and parallel programming.
This package object contains primitives for concurrent and parallel programming.
A more detailed guide to Futures and Promises, including discussion and examples can be found at http://docs.scala-lang.org/overviews/core/futures.html.
When working with Futures, you will often find that importing the whole concurrent package is convenient, furthermore you are likely to need an implicit ExecutionContext in scope for many operations involving Futures and Promises:
import scala.concurrent._ import ExecutionContext.Implicits.global
Operations often require a duration to be specified. A duration DSL is available to make defining these easier:
import scala.concurrent.duration._ val d: Duration = 10.seconds
Basic use of futures is easy with the factory method on Future, which executes a provided function asynchronously, handing you back a future result of that function without blocking the current thread. In order to create the Future you will need either an implicit or explicit ExecutionContext to be provided:
import scala.concurrent._ import ExecutionContext.Implicits.global // implicit execution context val firstZebra: Future[Int] = Future { val source = scala.io.Source.fromFile("/etc/dictionaries-common/words") source.toSeq.indexOfSlice("zebra") }
Although blocking is possible in order to await results (with a mandatory timeout duration):
import scala.concurrent.duration._ Await.result(firstZebra, 10.seconds)
and although this is sometimes necessary to do, in particular for testing purposes, blocking in general is discouraged when working with Futures and concurrency in order to avoid potential deadlocks and improve performance. Instead, use callbacks or combinators to remain in the future domain:
val animalRange: Future[Int] = for { aardvark <- firstAardvark zebra <- firstZebra } yield zebra - aardvark animalRange.onSuccess { case x if x > 500000 => println("It's a long way from Aardvark to Zebra") }
The scala.language
object controls the language features available to the programmer, as proposed in the
SIP-18 document.
The scala.language
object controls the language features available to the programmer, as proposed in the
SIP-18 document.
Each of these features has to be explicitly imported into the current scope to become available:
import language.postfixOps // or language._ List(1, 2, 3) reverse
The language features are:
dynamics
enables defining calls rewriting using the Dynamic
traitpostfixOps
enables postfix operatorsreflectiveCalls
enables using structural typesimplicitConversions
enables defining implicit methods and membershigherKinds
enables writing higher-kinded typesexistentials
enables writing existential typesexperimental
contains newer features that have not yet been tested in production
The package object scala.math
contains methods for performing basic
numeric operations such as elementary exponential, logarithmic, root and
trigonometric functions.
The package object scala.sys
contains methods for reading
and altering core aspects of the virtual machine as well as the
world outside of it.
The package object scala.sys
contains methods for reading
and altering core aspects of the virtual machine as well as the
world outside of it.
2.9
2.9
This object contains utility methods to build responders.
This object contains utility methods to build responders.
(Since version 2.11.0) This object will be removed
1.0
2.1
class Responder
Core Scala types. They are always available without an explicit import.