Scala Reference
Scala Reference
Specification
Version 2.9
Martin Odersky
1 Lexical Syntax 3
1.1 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Newline Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.3.1 Integer Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3.2 Floating Point Literals . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3.3 Boolean Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.4 Character Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.5 String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.6 Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.3.7 Symbol literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4 Whitespace and Comments . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.5 XML mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3 Types 19
3.1 Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
3.2 Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.2.1 Singleton Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.2.2 Type Projection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.2.3 Type Designators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.2.4 Parameterized Types . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.2.5 Tuple Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.2.6 Annotated Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.2.7 Compound Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.2.8 Infix Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.2.9 Function Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
iv CONTENTS
5.2 Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
5.3 Class Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.3.1 Constructor Definitions . . . . . . . . . . . . . . . . . . . . . . . . 66
5.3.2 Case Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
5.3.3 Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
5.4 Object Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
6 Expressions 73
6.1 Expression Typing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
6.2 Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
6.3 The Null Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
6.4 Designators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
6.5 This and Super . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
6.6 Function Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
6.6.1 Named and Default Arguments . . . . . . . . . . . . . . . . . . . . 79
6.7 Method Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
6.8 Type Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
6.9 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
6.10 Instance Creation Expressions . . . . . . . . . . . . . . . . . . . . . . . . 81
6.11 Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
6.12 Prefix, Infix, and Postfix Operations . . . . . . . . . . . . . . . . . . . . . 83
6.12.1 Prefix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
6.12.2 Postfix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
6.12.3 Infix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
6.12.4 Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . 85
6.13 Typed Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
6.14 Annotated Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
6.15 Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
6.16 Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
6.17 While Loop Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
6.18 Do Loop Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
6.19 For Comprehensions and For Loops . . . . . . . . . . . . . . . . . . . . . 89
6.20 Return Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
vi CONTENTS
Preface
Lexical Syntax
Scala programs are written using the Unicode Basic Multilingual Plane (BMP) char-
acter set; Unicode supplementary characters are not presently supported. This
chapter defines the two modes of Scala’s lexical syntax, the Scala mode and the XML
mode. If not otherwise mentioned, the following descriptions of Scala tokens refer
to Scala mode, and literal characters ‘c’ refer to the ASCII fragment \u0000-\u007F.
In Scala mode, Unicode escapes are replaced by the corresponding Unicode charac-
ter with the given hexadecimal code.
1.1 Identifiers
Syntax:
There are three ways to form an identifier. First, an identifier can start with a letter
which can be followed by an arbitrary sequence of letters and digits. This may be
followed by underscore ‘_’ characters and another string composed of either letters
and digits or of operator characters. Second, an identifier can start with an operator
character followed by an arbitrary sequence of operator characters. The preceding
two forms are called plain identifiers. Finally, an identifier may also be formed by an
arbitrary string between back-quotes (host systems may impose some restrictions
on which strings are legal for identifiers). The identifier then is composed of all
characters excluding the backquotes themselves.
As usual, a longest match rule applies. For instance, the string
big_bob++=‘def‘
decomposes into the three identifiers big_bob, ++=, and def. The rules for pattern
matching further distinguish between variable identifiers, which start with a lower
case letter, and constant identifiers, which do not.
The ‘$’ character is reserved for compiler-synthesized identifiers. User programs
should not define identifiers which contain ‘$’ characters.
The following names are reserved words instead of being members of the syntactic
class id of lexical identifiers.
The Unicode operators \u21D2 ‘⇒’ and \u2190 ‘←’, which have the ASCII equiva-
1.2 Newline Characters 5
Example 1.1.2 Backquote-enclosed strings are a solution when one needs to ac-
cess Java identifiers that are reserved words in Scala. For instance, the statement
Thread.yield() is illegal, since yield is a reserved word in Scala. However, here’s a
work-around:
Thread.‘yield‘()
Syntax:
The tokens that can terminate a statement are: literals, identifiers and the following
delimiters and reserved words:
The tokens that can begin a statement are all Scala tokens except the following de-
limiters and reserved words:
A case token can begin a statement only if followed by a class or object token.
Newlines are enabled in:
6 Lexical Syntax
1. all of a Scala source file, except for nested regions where newlines are disabled,
and
2. the interval between matching { and } brace tokens, except for nested regions
where newlines are disabled.
1. the interval between matching ( and ) parenthesis tokens, except for nested
regions where newlines are enabled, and
2. the interval between matching [ and ] bracket tokens, except for nested re-
gions where newlines are enabled.
3. The interval between a case token and its matching => token, except for
nested regions where newlines are enabled.
4. Any regions analyzed in XML mode (§1.5).
Note that the brace characters of {...} escapes in XML and string literals are not
tokens, and therefore do not enclose a region where newlines are enabled.
Normally, only a single nl token is inserted between two consecutive non-newline
tokens which are on different lines, even if there are multiple lines between the two
tokens. However, if two tokens are separated by at least one completely blank line
(i.e a line which contains no printable characters), then two nl tokens are inserted.
The Scala grammar (given in full in Appendix A) contains productions where op-
tional nl tokens, but not semicolons, are accepted. This has the effect that a new-
line in one of these positions does not terminate an expression or statement. These
positions can be summarized as follows:
Multiple newline tokens are accepted in the following places (note that a semicolon
in place of the newline would be illegal in every one of these cases):
Example 1.2.1 The following code contains four well-formed statements, each on
two lines. The newline tokens between the two lines are not treated as statement
separators.
if (x > 0)
x = x - 1
while (x > 0)
x = x / 2
type
IntList = List[Int]
new Iterator[Int]
{
private var x = 0
def hasNext = true
def next = { x += 1; x }
}
With an additional newline character, the same code is interpreted as an object cre-
ation followed by a local block:
new Iterator[Int]
{
private var x = 0
def hasNext = true
def next = { x += 1; x }
}
x < 0 ||
x > 10
With an additional newline character, the same code is interpreted as two expres-
sions:
8 Lexical Syntax
x < 0 ||
x > 10
Example 1.2.4 The following code designates a single, curried function definition:
(y: Int) = x + y
@serializable
protected class Data { ... }
@serializable
1.3 Literals
There are literals for integer numbers, floating point numbers, characters, booleans,
symbols, strings. The syntax of these literals is in each case as in Java.
Syntax:
Syntax:
Integer literals are usually of type Int, or of type Long when followed by a L or l
suffix. Values of type Int are all integer numbers between −231 and 231 − 1, inclu-
sive. Values of type Long are all integer numbers between −263 and 263 −1, inclusive.
A compile-time error occurs if an integer literal denotes a number outside these
ranges.
However, if the expected type pt (§6.1) of a literal in an expression is either Byte,
Short, or Char and the integer number fits in the numeric range defined by the type,
then the number is converted to type pt and the literal’s type is pt. The numeric
ranges given by these types are:
Byte −27 to 27 − 1
Short −215 to 215 − 1
Char 0 to 216 − 1
0 21 0xFFFFFFFF -42L
Syntax:
Floating point literals are of type Float when followed by a floating point type suffix
F or f, and are of type Double otherwise. The type Float consists of all IEEE 754 32-
bit single-precision binary floating point values, whereas the type Double consists
of all IEEE 754 64-bit double-precision binary floating point values.
If a floating point literal in a program is followed by a token starting with a letter,
there must be at least one intervening whitespace character between the two to-
kens.
10 Lexical Syntax
Example 1.3.3 The phrase ‘1.toString’ parses as three tokens: the integer literal
‘1’, a ‘.’, and the identifier ‘toString’.
Example 1.3.4 ‘1.’ is not a valid floating point literal, because the mandatory digit
after the ‘.’ is missing.
Syntax:
The boolean literals true and false are members of type Boolean.
Syntax:
Note that ‘\u000A’ is not a valid character literal because Unicode conversion is done
before literal parsing and the Unicode character \u000A (line feed) is not a printable
character. One can use instead the escape sequence ‘\n’ or the octal escape ‘\12’
(§1.3.6).
Syntax:
A string literal is a sequence of characters in double quotes. The characters are ei-
ther printable unicode character or are described by escape sequences (§1.3.6). If
1.3 Literals 11
the string literal contains a double quote character, it must be escaped, i.e. \". The
value of a string literal is an instance of class String.
"Hello,\nWorld!"
"This string contains a \" character."
Syntax:
The Scala library contains a utility method stripMargin which can be used to strip
leading whitespace from multi-line strings. The expression
evaluates to
The following escape sequences are recognized in character and string literals.
\b \u0008: backspace BS
\t \u0009: horizontal tab HT
\n \u000a: linefeed LF
\f \u000c: form feed FF
\r \u000d: carriage return CR
\" \u0022: double quote "
' \u0027: single quote ’
\\ \u005c: backslash \
A character with Unicode between 0 and 255 may also be represented by an octal
escape, i.e. a backslash ‘\’ followed by a sequence of up to three octal characters.
It is a compile time error if a backslash character in a character or string literal does
not start a valid escape sequence.
Syntax:
package scala
final case class Symbol private (name: String) {
override def toString: String = "’" + name
}
The apply method of Symbol’s companion object caches weak references to Symbols,
thus ensuring that identical symbol literals are equivalent with respect to reference
equality.
In order to allow literal inclusion of XML fragments, lexical analysis switches from
Scala mode to XML mode when encountering an opening angle bracket ’<’ in the
following circumstance: The ’<’ must be preceded either by whitespace, an opening
parenthesis or an opening brace and immediately followed by a character starting
an XML name.
Syntax:
XNameStart ::= ‘_’ | BaseChar | Ideographic (as in W3C XML, but without ‘:’
• the XML expression or the XML pattern started by the initial ’<’ has been suc-
cessfully parsed, or if
• the parser encounters an embedded Scala expression or pattern and forces
the Scanner back to normal mode, until the Scala expression or pattern is suc-
cessfully parsed. In this case, since code and XML fragments can be nested,
the parser has to maintain a stack that reflects the nesting of XML and Scala
expressions adequately.
Note that no Scala tokens are constructed in XML mode, and that comments are
interpreted as text.
Example 1.5.1 The following value definition uses an XML literal with two embed-
ded Scala expressions
val b = <book>
<title>The Scala Language Specification</title>
<version>{scalaBook.version}</version>
<authors>{scalaBook.authors.mkList("", ", ", "")}</authors>
</book>
Chapter 2
Names in Scala identify types, values, methods, and classes which are collectively
called entities. Names are introduced by local definitions and declarations (§4), in-
heritance (§5.1.3), import clauses (§4.7), or package clauses (§9.2) which are collec-
tively called bindings.
Bindings of different kinds have a precedence defined on them:
There are two different name spaces, one for types (§3) and one for terms (§6). The
same name may designate a type and a term, depending on the context where the
name is used.
A binding has a scope in which the entity defined by a single name can be accessed
using a simple name. Scopes are nested. A binding in some inner scope shadows
bindings of lower precedence in the same scope as well as bindings of the same or
lower precedence in outer scopes.
Note that shadowing is only a partial order. In a situation like
val x = 1;
{ import p.x;
x }
16 Identifiers, Names and Scopes
neither binding of x shadows the other. Consequently, the reference to x in the third
line above would be ambiguous.
A reference to an unqualified (type- or term-) identifier x is bound by the unique
binding, which
• defines an entity with name x in the same namespace as the identifier, and
• shadows all other bindings that define entities with name x in that names-
pace.
Example 2.0.2 Assume the following two definitions of a objects named X in pack-
ages P and Q.
package P {
object X { val x = 1; val y = 2 }
}
package Q {
object X { val x = true; val y = "" }
}
The following program illustrates different kinds of bindings and precedences be-
tween them.
A reference to a qualified (type- or term-) identifier e.x refers to the member of the
type T of e which has the name x in the same namespace as the identifier. It is
an error if T is not a value type (§3.2). The type of e.x is the member type of the
referenced entity in T .
Chapter 3
Types
Syntax:
We distinguish between first-order types and type constructors, which take type pa-
rameters and yield types. A subset of first-order types called value types represents
sets of (first-class) values. Value types are either concrete or abstract.
Every concrete value type can be represented as a class type, i.e. a type designator
(§3.2.3) that refers to a a class or a trait1 (§5.3), or as a compound type (§3.2.7) rep-
resenting an intersection of types, possibly with a refinement (§3.2.7) that further
constrains the types of its members. Abstract value types are introduced by type
1
We assume that objects and packages also implicitly define a class (of the same name as the
object or package, but inaccessible to user programs).
20 Types
parameters (§4.4) and abstract type bindings (§4.3). Parentheses in types can be
used for grouping.
Non-value types capture properties of identifiers that are not values (§3.3). For ex-
ample, a type constructor (§3.3.3) does not directly specify a type of values. How-
ever, when a type constructor is applied to the correct type arguments, it yields a
first-order type, which may be a value type.
Non-value types are expressed indirectly in Scala. E.g., a method type is described
by writing down a method signature, which in itself is not a real type, although it
gives rise to a corresponding method type (§3.3.1). Type constructors are another
example, as one can write type Swap[m[_, _], a,b] = m[b, a], but there is no
syntax to write the corresponding anonymous type function directly.
3.1 Paths
Syntax:
Paths are not types themselves, but they can be a part of named types and in that
function form a central role in Scala’s type system.
A path is one of the following.
• C .this, where C references a class. The path this is taken as a shorthand for
C .this where C is the name of the class directly enclosing the reference.
Every value in Scala has a type which is of one of the following forms.
Syntax:
A singleton type is of the form p .type, where p is a path pointing to a value expected
to conform (§6.1) to scala.AnyRef. The type denotes the set of values consisting of
null and the value denoted by p.
Syntax:
Syntax:
A type designator refers to a named value type. It can be simple or qualified. All
such type designators are shorthands for type projections.
Specifically, the unqualified type name t where t is bound in some class, object, or
package C is taken as a shorthand for C .this.type#t . If t is not bound in a class,
object, or package, then t is taken as a shorthand for ².type#t .
A qualified type designator has the form p .t where p is a path (§3.1) and t is a type
name. Such a type designator is equivalent to the type projection p .type#t .
Example 3.2.1 Some type designators and their expansions are listed below. We
assume a local type parameter t , a value maintable with a type member Node and
the standard class scala.Int,
t ².type#t
Int scala.type#Int
scala.Int scala.type#Int
data.maintable.Node data.maintable.type#Node
22 Types
Syntax:
class F[M[_], X] { . . . }
class S[K <: String] { . . . }
class G[M[ Z <: I ], I] { . . . }
TreeMap[I, String]
List[I]
List[List[Boolean]]
F[List, Int]
G[S, String]
Example 3.2.3 Given the type definitions of Example 3.2.2, the following types are
ill-formed:
Syntax:
A tuple type (T1 , . . . , Tn ) is an alias for the class scala.Tuplen [T1 , . . . , Tn ], where
n ≥ 2.
Tuple classes are case classes whose fields can be accessed using selectors _1, ..., _n.
Their functionality is abstracted in a corresponding Product trait. The n-ary tuple
class and product trait are defined at least as follows in the standard Scala library
(they might also add other methods and implement other traits).
Syntax:
Example 3.2.4 The following type adds the @suspendable@ annotation to the type
String:
String @suspendable
Syntax:
Example 3.2.5 The following example shows how to declare and use a function
which parameter’s type contains a refinement with structural declarations.
Although Bird and Plane do not share any parent class other than Object, the
parameter r of function takeoff is defined using a refinement with structural dec-
larations to accept any object that declares a value callsign and a fly function.
2
A reference to a structurally defined member (method call or access to a value or variable) may
generate binary code that is significantly slower than an equivalent code to a non-structural member.
3.2 Value Types 25
Syntax:
Syntax:
The type (T1 , . . . , Tn ) => U represents the set of function values that take argu-
ments of types T1 , . . . , Tn and yield results of type U . In the case of exactly one
argument type T => U is a shorthand for (T ) => U . An argument type of the
form => T represents a call-by-name parameter (§4.6.1) of type T .
Function types associate to the right, e.g. S => T => U is the same as
S => (T => U ).
Function types are shorthands for class types that define apply functions. Specif-
ically, the n-ary function type (T1 , . . . , Tn ) => U is a shorthand for the class type
Functionn [T1 , . . . , Tn ,U ]. Such class types are defined in the Scala library for n
between 0 and 9 as follows.
package scala
trait Functionn [-T1 , . . . , -Tn , +R ] {
def apply(x 1 : T1 , . . . , x n : Tn ): R
override def toString = "<function>"
}
Hence, function types are covariant (§4.5) in their result type and contravariant in
26 Types
Syntax:
Simplification Rules
Syntax:
Scala supports a placeholder syntax for existential types. A wildcard type is of the
form _ >: L <: U . Both bound clauses may be omitted. If a lower bound clause >: L
is missing, >: scala.Nothing is assumed. If an upper bound clause <: U is miss-
ing, <: scala.Any is assumed. A wildcard type is a shorthand for an existentially
quantified type variable, where the existential quantification is implicit.
A wildcard type must appear as type argument of a parameterized type. Let T =
p.c[targs, T, targs0 ] be a parameterized type where targs, targs0 may be empty and T
is a wildcard type _ >: L <: U . Then T is equivalent to the existential type
where t is some fresh type variable. Wildcard types may also appear as parts of infix
types (§3.2.8), function types (§3.2.9), or tuple types (§3.2.5). Their expansion is
then the expansion in the equivalent parameterized type.
class Ref[T]
abstract class Outer { type T } .
The last two types in this list are equivalent. An alternative formulation of the first
type above using wildcard syntax is:
class List[+T]
The type
The types explained in the following do not denote sets of values, nor do they appear
explicitly in programs. They are introduced in this report as the internal types of
defined identifiers.
def a: Int
def b (x: Int): Boolean
def c (x: Int) (y: String, z: String): String
a: => Int
b: (Int) Boolean
c: (Int) (String, String) String
trait Iterable[+X] {
def flatMap[newType[+X] <: Iterable[X], S](f: X => newType[S]): newType[S]
}
Conceptually, the type constructor Iterable is a name for the anonymous type
[+X] Iterable[X], which may be passed to the newType type constructor param-
eter in flatMap.
Types of class members depend on the way the members are referenced. Central
here are three notions, namely:
30 Types
• The base types of a class type C with parents T1 , . . . , Tn are C itself, as well as
the base types of the compound type T1 with . . . with Tn {R }.
• The base types of an aliased type are the base types of its alias.
• The base types of an abstract type are the base types of its upper bound.
• The base types of a parameterized type C [T1 , . . . , Tn ] are the base types of
type C , where every occurrence of a type parameter a i of C has been replaced
by the corresponding parameter type Ti .
• The base types of a singleton type p .type are the base types of the type of p.
• The base types of a compound type T1 with . . . with Tn {R } are the re-
duced union of the base classes of all Ti ’s. This means: Let the multi-set S
be the multi-set-union of the base types of all Ti ’s. If S contains several type
instances of the same class, say S i #C [T1i , . . . , Tni ] (i ∈ I ), then all those in-
stances are replaced by one of them which conforms to all others. It is an
error if no such instance exists. It follows that the reduced union, if it exists,
produces a set of class types, where different types are instances of different
classes.
• The base types of a type selection S #T are determined as follows. If T is an
alias or abstract type, the previous clauses apply. Otherwise, T must be a (pos-
sibly parameterized) class type, which is defined in some class B . Then the
base types of S #T are the base types of T in B seen from the prefix type S.
• The base types of an existential type T forSome { Q } are all types
S forSome { Q } where S is a base type of T .
2. The notion of a type T in class C seen from some prefix type S makes sense only if
the prefix type S has a type instance of class C as a base type, say S 0 #C [T1 , . . . , Tn ].
Then we define as follows.
• If T is some other type, then the described mapping is performed to all its
type components.
Equivalence (≡) between types is the smallest congruence3 such that the following
holds:
3.5.2 Conformance
The conformance relation (<:) is the smallest transitive relation that satisfies the
following conditions.
• For every class type T such that T <: scala.AnyRef and not T <:
scala.NotNull one has scala.Null <: T .
• A type variable or abstract type t conforms to its upper bound and its lower
bound conforms to t .
• A class type or parameterized type conforms to any of its base-types.
• A singleton type p .type conforms to the type of the path p.
• A singleton type p .type conforms to the type scala.Singleton.
3.5 Relations between types 33
The (<:) relation forms pre-order between types, i.e. it is transitive and reflexive.
least upper bounds and greatest lower bounds of a set of types are understood to be
relative to that order.
Note. The least upper bound or greatest lower bound of a set of types does not
always exist. For instance, consider the class definitions
class A[+T] {}
class B extends A[B]
class C extends A[C]
Then the types A[Any], A[A[Any]], A[A[A[Any]]], ... form a descending se-
quence of upper bounds for B and C. The least upper bound would be the infinite
limit of that sequence, which does not exist as a Scala type. Since cases like this are
in general impossible to detect, a Scala compiler is free to reject a term which has
a type specified as a least upper or greatest lower bound, and that bound would be
more complex than some compiler-set limit4 .
The least upper bound or greatest lower bound might also not be unique. For in-
stance A with B and B with A are both greatest lower of A and B. If there are several
least upper bounds or greatest lower bounds, the Scala compiler is free to pick any
one of them.
In some situations Scala uses a more genral conformance relation. A type S weakly
conforms to a type T , written S <:w T , if S <: T or both S and T are primitive number
types and S precedes T in the following ordering.
4
The current Scala compiler limits the nesting level of parameterization in such bounds to be at
most two deeper than the maximum nesting level of the operand types
3.6 Volatile Types 35
A weak least upper bound is a least upper bound with respect to weak conformance.
Type volatility approximates the possibility that a type parameter or abstract type
instance of a type does not have any non-null values. As explained in (§3.1), a value
member of a volatile type cannot appear in a path.
A type is volatile if it falls into one of four categories:
A compound type T1 with . . . with Tn {R } is volatile if one of the following two
conditions hold.
A type is called generic if it contains type arguments or type variables. Type erasure
is a mapping from (possibly generic) types to non-generic types. We write |T | for
the erasure of type T . The erasure mapping is defined as follows.
Syntax:
A declaration introduces names and assigns them types. It can form part of a class
definition (§5.1) or of a refinement in a compound type (§3.2.7).
A definition introduces names that denote terms or types. It can form part of an
object or class definition or it can be local to a block. Both declarations and defini-
tions produce bindings that associate type names with type definitions or bounds,
and that associate term names with types.
The scope of a name introduced by a declaration or definition is the whole state-
ment sequence containing the binding. However, there is a restriction on forward
references in blocks: In a statement sequence s 1 . . . s n making up a block, if a simple
name in s i refers to an entity defined by s j where j ≥ i , then for all s k between and
including s i and s j ,
Syntax:
final val x = e
where e is a constant expression (§6.24). The final modifier must be present and
no type annotation may be given. References to the constant value x are themselves
treated as constant expressions; in the generated code they are replaced by the def-
inition’s right-hand side e.
Value definitions can alternatively have a pattern (§8.1) as left-hand side. If p is
some pattern other than a simple name or a name followed by a colon and a type,
then the value definition val p = e is expanded as follows:
1. If the pattern p has bound variables x 1 , . . . , x n , where n > 1:
val pi = 3.1415
val pi: Double = 3.1415 // equivalent to first definition
val Some(x) = f() // a pattern definition
val x :: xs = mylist // an infix pattern definition
The name of any declared or defined value may not end in _=.
A value declaration val x 1 , . . . , x n : T is a shorthand for the sequence of value dec-
larations val x 1 : T ; ...; val x n : T . A value definition val p 1 , . . . , p n = e is
a shorthand for the sequence of value definitions val p 1 = e ; ...; val p n = e.
A value definition val p 1 , . . . , p n : T = e is a shorthand for the sequence of value
definitions val p 1 : T = e ; ...; val p n : T = e.
Syntax:
def x : T
def x _= ( y : T ): Unit
able definition var p = e where p is a pattern other than a simple name or a name
followed by a colon and a type is expanded in the same way (§4.1) as a value defi-
nition val p = e, except that the free names in p are introduced as mutable vari-
ables, not values.
The name of any declared or defined variable may not end in _=.
A variable definition var x : T = _ can appear only as a member of a template. It
introduces a mutable field with type T and a default initial value. The default value
depends on the type T as follows:
When they occur as members of a template, both forms of variable definition also
introduce a getter function x which returns the value currently assigned to the vari-
able, as well as a setter function x _= which changes the value currently assigned to
the variable. The functions have the same signatures as for a variable declaration.
The template then has these getter and setter functions as members, whereas the
original variable cannot be accessed directly as a template member.
Example 4.2.1 The following example shows how properties can be simulated in
Scala. It defines a class TimeOfDayVar of time values with updatable integer fields
representing hours, minutes, and seconds. Its implementation contains tests that
allow only legal values to be assigned to these fields. The user code, on the other
hand, accesses these fields just like normal variables.
class TimeOfDayVar {
private var h: Int = 0
private var m: Int = 0
private var s: Int = 0
def hours = h
def hours_= (h: Int) = if (0 <= h && h < 24) this.h = h
else throw new DateError()
def minutes = m
def minutes_= (m: Int) = if (0 <= m && m < 60) this.m = m
else throw new DateError()
def seconds = s
def seconds_= (s: Int) = if (0 <= s && s < 60) this.s = s
4.3 Type Declarations and Type Aliases 41
Syntax:
A type declaration type t [tps ] >: L <: U declares t to be an abstract type with
lower bound type L and upper bound type U . If the type parameter clause [tps ] is
omitted, t abstracts over a first-order type, otherwise t stands for a type constructor
that accepts type arguments as described by the type parameter clause.
If a type declaration appears as a member declaration of a type, implementations
of the type may implement t with any type T for which L <: T <: U . It is a compile-
time error if L does not conform to U . Either or both bounds may be omitted. If the
lower bound L is absent, the bottom type scala.Nothing is assumed. If the upper
bound U is absent, the top type scala.Any is assumed.
A type constructor declaration imposes additional restrictions on the concrete types
for which t may stand. Besides the bounds L and U , the type parameter clause
may impose higher-order bounds and variances, as governed by the conformance
of type constructors (§3.5.2).
The scope of a type parameter extends over the bounds >: L <: U and the type
parameter clause tps itself. A higher-order type parameter clause (of an abstract
type constructor t c) has the same kind of scope, restricted to the declaration of the
type parameter t c.
To illustrate nested scoping, these declarations are all equivalent:
type t[m[x] <: Bound[x], Bound[x]], type t[m[x] <: Bound[x], Bound[y]]
and type t[m[x] <: Bound[x], Bound[_]], as the scope of, e.g., the type param-
42 Basic Declarations and Definitions
Example 4.3.1 The following are legal type declarations and definitions:
If a type alias type t [tps ] = S refers to a class type S, the name t can also be used
as a constructor for objects of type S.
Example 4.3.2 The Predef object contains a definition which establishes Pair as
an alias of the parameterized class Tuple2:
As a consequence, for any two types S and T , the type Pair[S , T ] is equivalent to
the type Tuple2[S , T ]. Pair can also be used as a constructor instead of Tuple2,
as in:
Syntax:
Type parameters appear in type definitions, class definitions, and function defini-
tions. In this section we consider only type parameter definitions with lower bounds
>: L and upper bounds <: U whereas a discussion of context bounds : U and
view bounds <% U is deferred to Section 7.4.
The most general form of a first-order type parameter is
@a 1 . . . @a n ± t >: L <: U . Here, L, and U are lower and upper bounds that
constrain possible type arguments for the parameter. It is a compile-time error if L
does not conform to U . ± is a variance, i.e. an optional prefix of either +, or -. One
or more annotations may precede the type parameter.
The names of all type parameters must be pairwise different in their enclosing type
parameter clause. The scope of a type parameter includes in each case the whole
type parameter clause. Therefore it is possible that a type parameter appears as
part of its own bounds or the bounds of other type parameters in the same clause.
However, a type parameter may not be bounded directly or indirectly by itself.
A type constructor parameter adds a nested type parameter clause to the
type parameter. The most general form of a type constructor parameter is
@a 1 . . . @a n ± t [tps ] >: L <: U .
The above scoping restrictions are generalized to the case of nested type parameter
clauses, which declare higher-order type parameters. Higher-order type parame-
ters (the type parameters of a type parameter t ) are only visible in their immediately
surrounding parameter clause (possibly including clauses at a deeper nesting level)
and in the bounds of t . Therefore, their names must only be pairwise different from
the names of other visible parameters. Since the names of higher-order type pa-
rameters are thus often irrelevant, they may be denoted with a ‘_’, which is nowhere
visible.
[S, T]
44 Basic Declarations and Definitions
[@specialized T, U]
[Ex <: Throwable]
[A <: Comparable[B], B <: A]
[A, B >: A, C >: A <: B]
[M[X], N[X]]
[M[_], N[_]] // equivalent to previous clause
[M[X <: Bound[X]], Bound[_]]
[M[+X] <: Iterable[X]]
Variance annotations indicate how instances of parameterized types vary with re-
spect to subtyping (§3.5.2). A ‘+’ variance indicates a covariant dependency, a ‘-’
variance indicates a contravariant dependency, and a missing variance indication
indicates an invariant dependency.
A variance annotation constrains the way the annotated type variable may ap-
pear in the type or class which binds the type parameter. In a type definition
type T [tps ] = S, or a type declaration type T [tps ] >: L <: U type parameters
labeled ‘+’ must only appear in covariant position whereas type parameters labeled
‘-’ must only appear in contravariant position. Analogously, for a class definition
class C [tps ](ps ) extends T { x : S => ...}, type parameters labeled ‘+’ must
only appear in covariant position in the self type S and the template T , whereas type
parameters labeled ‘-’ must only appear in contravariant position.
The variance position of a type parameter in a type or template is defined as follows.
Let the opposite of covariance be contravariance, and the opposite of invariance be
itself. The top-level of the type or template is always in covariant position. The
variance position changes at the following constructs.
With this variance annotation, type instances of P subtype covariantly with respect
to their arguments. For instance,
If the members of P are mutable variables, the same variance annotation becomes
illegal.
If the mutable variables are object-private, the class definition becomes legal again:
Example 4.5.2 The following variance annotation is illegal, since a appears in con-
travariant position in the parameter of append:
The problem can be avoided by generalizing the type of append by means of a lower
bound:
Syntax:
A function declaration has the form def f psig: T , where f is the function’s
name, psig is its parameter signature and T is its result type. A function definition
def f psig: T = e also includes a function body e, i.e. an expression which defines
the function’s result. A parameter signature consists of an optional type parameter
clause [tps ], followed by zero or more value parameter clauses (ps1 ). . .(psn ). Such
a declaration or definition introduces a value with a (possibly polymorphic) method
type whose parameter types and result type are as given.
4.6 Function Declarations and Definitions 47
The type of the function body is expected to conform (§6.1) to the function’s de-
clared result type, if one is given. If the function definition is not recursive, the re-
sult type may be omitted, in which case it is determined from the packed type of the
function body.
A type parameter clause tps consists of one or more type declarations (§4.3), which
introduce type parameters, possibly with bounds. The scope of a type parameter
includes the whole signature, including any of the type parameter bounds as well as
the function body, if it is present.
A value parameter clause ps consists of zero or more formal parameter bindings
such as x : T or x : T = e, which bind value parameters and associate them with
their types. Each value parameter declaration may optionally define a default argu-
ment. The default argument expression e is type-checked with an expected type T 0
obtained by replacing all occurences of the function’s type parameters in T by the
undefined type.
For every parameter p i , j with a default argument a method named f $default$n is
generated which computes the default argument expression. Here, n denotes the
parameter’s position in the method declaration. These methods are parametrized
by the type parameter clause [tps ] and all value parameter clauses (ps1 ). . .(psi −1 )
preceeding p i , j . The f $default$n methods are inaccessible for user programs.
The scope of a formal value parameter name x comprises all subsequent parameter
clauses, as well as the method return type and the function body, if they are given.1
Both type parameter names and value parameter names must be pairwise distinct.
Syntax:
The type of a value parameter may be prefixed by =>, e.g. x : => T . The type of
such a parameter is then the parameterless method type => T . This indicates that
1
However, at present singleton types of method parameters may only appear in the method
body; so dependent method types are not supported.
48 Basic Declarations and Definitions
Syntax:
The last value parameter of a parameter section may be suffixed by “*”, e.g.
(..., x :T *). The type of such a repeated parameter inside the method is then
the sequence type scala.Seq[T ]. Methods with repeated parameters T * take
a variable number of arguments of type T . That is, if a method m with type
(p 1 : T1 , . . . , p n : Tn , p s : S *)U is applied to arguments (e 1 , . . . , e k ) where k ≥ n, then
m is taken in that application to have type (p 1 : T1 , . . . , p n : Tn , p s : S, . . . , p s 0 S)U ,
with k − n occurrences of type S where any parameter names beyond p s are
fresh. The only exception to this rule is if the last argument is marked to be
a sequence argument via a _* type annotation. If m above is applied to argu-
ments (e 1 , . . . , e n , e 0 : _*), then the type of m in that application is taken to be
(p 1 : T1 , . . . , p n : Tn , p s :scala.Seq[S ]).
It is not allowed to define any default arguments in a parameter section with a re-
peated parameter.
Example 4.6.3 The following method definition computes the sum of the squares
of a variable number of integer arguments.
sum()
sum(1)
4.6 Function Declarations and Definitions 49
sum(1, 2, 3)
val xs = List(1, 2, 3)
By contrast, the following application is well formed and yields again the result 6:
sum(xs: _*)
4.6.3 Procedures
Syntax:
Special syntax exists for procedures, i.e. functions that return the Unit value (). A
procedure declaration is a function declaration where the result type is omitted.
The result type is then implicitly completed to the Unit type. E.g., def f (ps) is
equivalent to def f (ps): Unit.
A procedure definition is a function definition where the result type and the equals
sign are omitted; its defining expression must be a block. E.g., def f (ps) {stats} is
equivalent to def f (ps): Unit = {stats}.
trait Writer {
def write(str: String)
}
object Terminal extends Writer {
def write(str: String) { System.out.println(str) }
}
trait Writer {
def write(str: String): Unit
}
object Terminal extends Writer {
def write(str: String): Unit = { System.out.println(str) }
}
50 Basic Declarations and Definitions
A class member definition m that overrides some other function m 0 in a base class
of C may leave out the return type, even if it is recursive. In this case, the return type
R 0 of the overridden function m 0 , seen as a member of C , is taken as the return type
of m for each recursive invocation of m. That way, a type R for the right-hand side
of m can be determined, which is then taken as the return type of m. Note that R
may be different from R 0 , as long as R conforms to R 0 .
trait I {
def factorial(x: Int): Int
}
class C extends I {
def factorial(x: Int) = if (x == 0) 1 else x * factorial(x - 1)
}
Here, it is OK to leave out the result type of factorial in C, even though the method
is recursive.
Syntax:
An import clause has the form import p . I where p is a stable identifier (§3.1) and
I is an import expression. The import expression determines a set of names of im-
portable members of p which are made available without qualification. A member
m of p is importable if it is not object-private (§5.2). The most general form of an
import expression is a list of import selectors
{ x 1 => y 1 , . . . , x n => y n , _ } .
for n ≥ 0, where the final wildcard ‘_’ may be absent. It makes available each im-
portable member p .x i under the unqualified name y i . I.e. every import selector
x i => y i renames p .x i to y i . If a final wildcard is present, all importable mem-
bers z of p other than x 1 , . . . , x n , y 1 , . . . , y n are also made available under their own
unqualified names.
Import selectors work in the same way for type and term members. For instance, an
4.7 Import Clauses 51
import clause import p .{x => y } renames the term name p .x to the term name
y and the type name p .x to the type name y. At least one of these two names must
reference an importable member of p.
If the target in an import selector is a wildcard, the import selector hides access to
the source member. For instance, the import selector x => _ “renames” x to the
wildcard symbol (which is unaccessible as a name in user programs), and thereby
effectively prevents unqualified access to x. This is useful if there is a final wild-
card in the same import selector list, which imports all members not mentioned in
previous import selectors.
The scope of a binding introduced by an import-clause starts immediately after the
import clause and extends to the end of the enclosing block, template, package
clause, or compilation unit, whichever comes first.
Several shorthands exist. An import selector may be just a simple name x. In
this case, x is imported without renaming, so the import selector is equivalent to
x => x. Furthermore, it is possible to replace the whole import selector list by
a single identifier or wildcard. The import clause import p .x is equivalent to
import p .{x } , i.e. it makes available without qualification the member x of p. The
import clause import p ._ is equivalent to import p .{_}, i.e. it makes available
without qualification all members of p (this is analogous to import p .* in Java).
An import clause with multiple import expressions import p 1 . I 1 , . . . , p n . I n is in-
terpreted as a sequence of import clauses import p 1 . I 1 ; . . .; import p n . I n .
object M {
def z = 0, one = 1
def add(x: Int, y: Int): Int = x + y
}
{ M.add(M.z, M.one) } .
Chapter 5
Syntax:
Classes (§5.3) and objects (§5.4) are both defined in terms of templates.
5.1 Templates
Syntax:
A template defines the type signature, behavior and initial state of a trait
or class of objects or of a single object. Templates form part of instance
creation expressions, class definitions, and object definitions. A template
sc with mt 1 with . . . with mt n {stats } consists of a constructor invocation sc
which defines the template’s superclass, trait references mt 1 , . . . , mt n (n ≥ 0), which
define the template’s traits, and a statement sequence stats which contains initial-
ization code and additional member definitions for the template.
Each trait reference mt i must denote a trait (§5.3.3). By contrast, the superclass
constructor sc normally refers to a class which is not a trait. It is possible to write
54 Classes and Objects
a list of parents that starts with a trait reference, e.g. mt 1 with . . . with mt n . In
that case the list of parents is implicitly extended to include the supertype of mt 1
as first parent type. The new supertype must have at least one constructor that
does not take parameters. In the following, we will always assume that this implicit
extension has been performed, so that the first parent class of a template is a regular
superclass constructor, not a trait reference.
The list of parents of a template must be well-formed. This means that the class
denoted by the superclass constructor sc must be a subclass of the superclasses
of all the traits mt 1 , . . . , mt n . In other words, the non-trait classes inherited by a
template form a chain in the inheritance hierarchy which starts with the template’s
superclass.
The least proper supertype of a template is the class type or compound type (§3.2.7)
consisting of all its parent class types.
The statement sequence stats contains member definitions that define new mem-
bers or overwrite members in the parent classes. If the template forms part of an
abstract class or trait definition, the statement part stats may also contain declara-
tions of abstract members. If the template forms part of a concrete class definition,
stats may still contain declarations of abstract type members, but not of abstract
term members. Furthermore, stats may in any case also contain expressions; these
are executed in the order they are given as part of the initialization of a template.
The sequence of template statements may be prefixed with a formal parameter def-
inition and an arrow, e.g. x =>, or x :T =>. If a formal parameter is given, it can
be used as an alias for the reference this throughout the body of the template. If
the formal parameter comes with a type T , this definition affects the self type S of
the underlying class or object as follows: Let C be the type of the class or trait or
object defining the template. If a type T is given for the formal self parameter, S is
the greatest lower bound of T and C . If no type T is given, S is just C . Inside the
template, the type of this is assumed to be S.
The self type of a class or object must conform to the self types of all classes which
are inherited by the template t .
A second form of self type annotation reads just this: S =>. It prescribes the type
S for this without introducing an alias name for it.
Inheriting from Java Types. A template may have a Java class as its superclass and
Java interfaces as its mixins.
Syntax:
Constructor invocations define the type, members, and initial state of objects cre-
ated by an instance creation expression, or of parts of an object’s definition which
are inherited by a class or object definition. A constructor invocation is a function
application x .c [targs](args1 ). . .(argsn ), where x is a stable identifier (§3.1), c is a
type name which either designates a class or defines an alias type for one, targs is a
type argument list, args1 , . . . , argsn are argument lists, and there is a constructor of
that class which is applicable (§6.6) to the given arguments. If the constructor invo-
cation uses named or default arguments, it is transformed into a block expression
using the same transformation as described in (§6.6.1).
The prefix ‘x .’ can be omitted. A type argument list can be given only if the class c
takes type parameters. Even then it can be omitted, in which case a type argument
list is synthesized using local type inference (§6.26.4). If no explicit arguments are
given, an empty list () is implicitly supplied.
56 Classes and Objects
The classes reachable through transitive closure of the direct inheritance relation
from a class C are called the base classes of C . Because of mixins, the inheritance re-
lationship on base classes forms in general a directed acyclic graph. A linearization
of this graph is defined as follows.
Definition 5.1.2 Let C be a class with template C 1 with ... with C n { stats }.
The linearization of C , L (C ) is defined as follows:
L (C ) = C , L (C n ) ~
+ ... ~
+ L (C 1 )
Here ~+ denotes concatenation where elements of the right operand replace identi-
cal elements of the left operand:
{a, A} ~
+B = a, (A ~
+ B ) ifa 6∈ B
= A~+B ifa ∈ B
Note that the linearization of a class refines the inheritance relation: if C is a sub-
class of D, then C precedes D in any linearization where both C and D occur. Defi-
nition 5.1.2 also satisfies the property that a linearization of a class always contains
the linearization of its direct superclass as a suffix. For instance, the linearization of
StringIterator is
which is a suffix of the linearization of its subclass Iter. The same is not true for the
linearization of mixins. For instance, the linearization of RichIterator is
Member definitions fall into two categories: concrete and abstract. Members of
class C are either directly defined (i.e. they appear in C ’s statement sequence stats)
or they are inherited. There are two rules that determine the set of members of a
class, one for each category:
overrides an abstract definition. Second, for definitions M and M ’ which are both
concrete or both abstract, M overrides M 0 if M appears in a class that precedes (in
the linearization of C ) the class in which M 0 is defined.
It is an error if a template directly defines two matching members. It is also an error
if a template contains two members (directly defined or inherited) with the same
name and the same erased type (§3.7). Finally, a template is not allowed to contain
two methods (directly defined or inherited) with the same name which both define
default arguments.
Then trait D has a directly defined abstract member h. It inherits member f from
trait C and member g from trait B.
5.1.4 Overriding
Let C be a class type. The inheritance closure of C is the smallest set S of types such
that
It is a static error if the inheritance closure of a class type consists of an infinite num-
ber of types. (This restriction is necessary to make subtyping decidable [KP07]).
Syntax:
A template may start with an early field definition clause, which serves to define
certain field values before the supertype constructor is called. In a template
60 Classes and Objects
{ val p 1 : T1 = e 1
...
val p n : Tn = e n
} with sc with mt 1 with mt n {stats }
The initial pattern definitions of p 1 , . . . , p n are called early definitions. They define
fields which form part of the template. Every early definition must define at least
one variable.
An early definition is type-checked and evaluated in the scope which is in effect
just before the template being defined, augmented by any type parameters of the
enclosing class and by any early definitions preceding the one being defined. In
particular, any reference to this in the right-hand side of an early definition refers
to the identity of this just outside the template. Consequently, it is impossible that
an early definition refers to the object being constructed by the template, or refers to
one of its fields and methods, except for any other preceding early definition in the
same section. Furthermore, references to preceding early definitions always refer to
the value that’s defined there, and do not take into account overriding definitions.
In other words, a block of early definitions is evaluated exactly as if it was a local
bock containing a number of value definitions.
Early definitions are evaluated in the order they are being defined before the super-
class constructor of the template is called.
Example 5.1.8 Early definitions are particularly useful for traits, which do not have
normal constructor parameters. Example:
trait Greeting {
val name: String
val msg = "How are you, "+name
}
class C extends {
val name = "Bob"
} with Greeting {
println(msg)
}
In the code above, the field name is initialized before the constructor of Greeting
is called. Therefore, field msg in class Greeting is properly initialized to
"How are you, Bob".
If name had been initialized instead in C’s normal class body, it would be initial-
ized after the constructor of Greeting. In that case, msg would be initialized to
"How are you, <null>".
5.2 Modifiers 61
5.2 Modifiers
Syntax:
Member definitions may be preceded by modifiers which affect the accessibility and
usage of the identifiers bound by them. If several modifiers are given, their order
does not matter, but the same modifier may not occur more than once. Modifiers
preceding a repeated definition apply to all constituent definitions. The rules gov-
erning the validity and meaning of a modifier are as follows.
• The private modifier can be used with any definition or declaration in a tem-
plate. Such members can be accessed only from within the directly enclos-
ing template and its companion module or companion class (§5.4). They are
not inherited by subclasses and they may not override definitions in parent
classes.
The modifier can be qualified with an identifier C (e.g. private[C ]) that must
denote a class or package enclosing the definition. Members labeled with
such a modifier are accessible respectively only from code inside the package
C or only from code inside the class C and its companion module (§5.4).
An different form of qualification is private[this]. A member M marked
with this modifier is called object-protected; it can be accessed only from
within the object in which it is defined. That is, a selection p.M is only le-
gal if the prefix is this or O .this, for some class O enclosing the reference. In
addition, the restrictions for unqualified private apply.
Members marked private without a qualifier are called class-private, whereas
members labeled with private[this] are called object-private. A member
is private if it is either class-private or object-private, but not if it is marked
private[C ] where C is an identifier; in the latter case the member is called
qualified private.
Class-private or object-private members may not be abstract, and may not
have protected or override modifiers.
• The protected modifier applies to class member definitions. Protected mem-
bers of a class can be accessed from within
62 Classes and Objects
– The access is within the template defining the member, or, if a qualifi-
cation C is given, inside the package C , or the class C , or its companion
module, or
– r is one of the reserved words this and super, or
– r ’s type conforms to a type-instance of the class which contains the ac-
cess.
members of the class. Only abstract classes and traits can have abstract term
members.
The abstract modifier can also be used in conjunction with override for
class member definitions. In that case the previous discussion applies.
• The final modifier applies to class member definitions and to class defini-
tions. A final class member definition may not be overridden in subclasses.
A final class may not be inherited by a template. final is redundant for ob-
ject definitions. Members of final classes or objects are implicitly also final, so
the final modifier is generally redundant for them, too. Note, however, that
constant value definitions (§4.1) do require an explicit final modifier, even if
they are defined in a final class or object. final may not be applied to incom-
plete members, and it may not be combined in one modifier list with sealed.
• The sealed modifier applies to class definitions. A sealed class may not be di-
rectly inherited, except if the inheriting template is defined in the same source
file as the inherited class. However, subclasses of a sealed class can be inher-
ited anywhere.
• The lazy modifier applies to value definitions. A lazy value is initialized the
first time it is accessed (which might never happen at all). Attempting to ac-
cess a lazy value during its initialization might lead to looping behavior. If
an exception is thrown during initialization, the value is considered uninitial-
ized, and a later access will retry to evaluate its right hand side.
Example 5.2.1 The following code illustrates the use of qualified private:
package outerpkg.innerpkg
class Outer {
class Inner {
private[Outer] def f()
private[innerpkg] def g()
private[outerpkg] def h()
}
}
Here, accesses to the method f can appear anywhere within OuterClass, but
not outside it. Accesses to method g can appear anywhere within the package
outerpkg.innerpkg, as would be the case for package-private methods in Java. Fi-
nally, accesses to method h can appear anywhere within package outerpkg, includ-
ing packages contained in it.
Example 5.2.2 A useful idiom to prevent clients of a class from constructing new
instances of that class is to declare the class abstract and sealed:
object m {
abstract sealed class C (x: Int) {
64 Classes and Objects
For instance, in the code above clients can create instances of class m.C only by call-
ing the nextC method of an existing m.C object; it is not possible for clients to create
objects of class m.C directly. Indeed the following two lines are both in error:
Syntax:
Here,
tps is a non-empty list of type parameters of the class being defined. The
scope of a type parameter is the whole class definition including the type pa-
rameter section itself. It is illegal to define two type parameters with the same
name. The type parameter section [tps ] may be omitted. A class with a type
parameter section is called polymorphic, otherwise it is called monomorphic.
which defines the base classes, behavior and initial state of objects of the
class. The extends clause extends sc with mt 1 with . . . with mt m can be
omitted, in which case extends scala.AnyRef is assumed. The class body
{stats } may also be omitted, in which case the empty body {} is assumed.
This class definition defines a type c [tps ] and a constructor which when applied to
parameters conforming to types ps initializes instances of type c [tps ] by evaluating
the template t .
Example 5.3.1 The following example illustrates val and var parameters of a class
C:
Example 5.3.2 The following class can be created only from its companion module.
object Sensitive {
def makeSensitive(credentials: Certificate): Sensitive =
if (credentials == Admin) new Sensitive()
66 Classes and Objects
Syntax:
A class may have additional constructors besides the primary constructor. These
are defined by constructor definitions of the form def this(ps1 ). . .(psn ) = e.
Such a definition introduces an additional constructor for the enclosing class, with
parameters as given in the formal parameter lists ps1 , . . . , psn , and whose evaluation
is defined by the constructor expression e. The scope of each formal parameter is
the subsequent parameter sections and the constructor expression e. A constructor
expression is either a self constructor invocation this(args1 ). . .(argsn ) or a block
which begins with a self constructor invocation. The self constructor invocation
must construct a generic instance of the class. I.e. if the class in question has name
C and type parameters [tps ], then a self constructor invocation must generate an
instance of C [tps ]; it is not permitted to instantiate formal type parameters.
The signature and the self constructor invocation of a constructor definition are
type-checked and evaluated in the scope which is in effect at the point of the en-
closing class definition, augmented by any type parameters of the enclosing class
and by any early definitions (§5.1.6) of the enclosing template. The rest of the con-
structor expression is type-checked and evaluated as a function body in the current
class.
If there are auxiliary constructors of a class C , they form together with C ’s primary
constructor (§5.3) an overloaded constructor definition. The usual rules for over-
loading resolution (§6.26.3) apply for constructor invocations of C , including for
the self constructor invocations in the constructor expressions themselves. How-
ever, unlike other methods, constructors are never inherited. To prevent infinite
cycles of constructor invocations, there is the restriction that every self constructor
invocation must refer to a constructor definition which precedes it (i.e. it must refer
to either a preceding auxiliary constructor or the primary constructor of the class).
class LinkedList[A]() {
var head = _
var tail = null
def isEmpty = tail != null
def this(head: A) = { this(); this.head = head }
def this(head: A, tail: List[A]) = { this(head); this.tail = tail }
}
This defines a class LinkedList with three constructors. The second constructor
constructs an singleton list, while the third one constructs a list with a given head
and tail.
Syntax:
If a class definition is prefixed with case, the class is said to be a case class.
The formal parameters in the first parameter section of a case class are called el-
ements; they are treated specially. First, the value of such a parameter can be ex-
tracted as a field of a constructor pattern. Second, a val prefix is implicitly added to
such a parameter, unless the parameter carries already a val or var modifier. Hence,
an accessor definition for the parameter is generated (§5.3).
A case class definition of c [tps ](ps1 ). . .(psn ) with type parameters tps and value
parameters ps implicitly generates an extractor object (§8.1.8) which is defined as
follows:
object c {
def apply[tps ](ps1 ). . .(psn ): c [tps ] = new c [Ts ](xs1 ). . .(xsn )
def unapply[tps ](x : c [tps ]) =
if (x eq null) scala.None
else scala.Some(x.xs11 , . . . , x.xs1k )
}
Here, Ts stands for the vector of types defined in the type parameter section
tps, each xsi denotes the parameter names of the parameter section psi , and
xs11 , . . . , xs1k denote the names of all parameters in the first parameter section xs1 .
If a type parameter section is missing in the class, it is also missing in the apply and
unapply methods. The definition of apply is omitted if class c is abstract.
If the case class definition contains an empty value parameter list, the unapply
method returns a Boolean instead of an Option type and is defined as follows:
The name of the unapply method is changed to unapplySeq if the first parameter
section ps1 of c ends in a repeated parameter of (§4.6.2). If a companion object
c exists already, no new object is created, but the apply and unapply methods are
added to the existing object instead.
A method named copy is implicitly added to every case class unless the class al-
ready has a member (directly defined or inherited) with that name, or the class has
a repeated parameter. The method is defined as follows:
Again, Ts stands for the vector of types defined in the type parameter section tps
and each xsi denotes the parameter names of the parameter section ps0i . The value
parameters ps01, j of first parameter list have the form x 1, j :T1, j =this.x 1, j , the other
parameters ps0i , j of the copy method are defined as x i , j :Ti , j . In all cases x i , j and Ti , j
refer to the name and type of the corresponding class parameter psi , j .
Every case class implicitly overrides some method definitions of class scala.AnyRef
(§12.1) unless a definition of the same method is already given in the case class itself
or a concrete definition of the same method is given in some base class of the case
class different from AnyRef. In particular:
Example 5.3.4 Here is the definition of abstract syntax for lambda calculus:
class Expr
case class Var (x: String) extends Expr
case class Apply (f: Expr, e: Expr) extends Expr
case class Lambda(x: String, e: Expr) extends Expr
This defines a class Expr with case classes Var, Apply and Lambda. A call-by-value
evaluator for lambda expressions could then be written as follows.
env(x)
case Apply(f, g) =>
val Value(Lambda (x, e1), env1) = eval(f, env)
val v = eval(g, env)
eval (e1, (y => if (y == x) v else env1(y)))
case Lambda(_, _) =>
Value(e, env)
}
It is possible to define further case classes that extend type Expr in other parts of the
program, for instance
This form of extensibility can be excluded by declaring the base class Expr sealed;
in this case, all classes that directly extend Expr must be in the same source file as
Expr.
5.3.3 Traits
Syntax:
A trait is a class that is meant to be added to some other class as a mixin. Unlike
normal classes, traits cannot have constructor parameters. Furthermore, no con-
structor arguments are passed to the superclass of the trait. This is not necessary as
traits are initialized after the superclass is initialized.
Assume a trait D defines some aspect of an instance x of type C (i.e. D is a base class
of C ). Then the actual supertype of D in x is the compound type consisting of all
the base classes in L (C ) that succeed D. The actual supertype gives the context for
resolving a super reference in a trait (§6.5). Note that the actual supertype depends
on the type to which the trait is added in a mixin composition; it is not statically
known at the time the trait is defined.
If D is not a trait, then its actual supertype is simply its least proper supertype (which
is statically known).
Example 5.3.5 The following trait defines the property of being comparable to ob-
jects of some type. It contains an abstract method < and default implementations
of the other comparison operators <=, >, and >=.
Example 5.3.6 Consider an abstract class Table that implements maps from a type
of keys A to a type of values B. The class has a method set to enter a new key /
value pair into the table, and a method get that returns an optional value matching
a given key. Finally, there is a method apply which is like get, except that it returns
a given default value if the table is undefined for the given key. This class is imple-
mented as follows.
Here is a trait that prevents concurrent access to the get and set operations of its
parent class:
Note that SynchronizedTable does not pass an argument to its superclass, Table,
even though Table is defined with a formal parameter. Note also that the super calls
in SynchronizedTable’s get and set methods statically refer to abstract methods in
class Table. This is legal, as long as the calling method is labeled abstract override
(§5.2).
Finally, the following mixin composition creates a synchronized list table with
strings as keys and integers as values and with a default value 0:
5.4 Object Definitions 71
The object MyTable inherits its get and set method from SynchronizedTable. The
super calls in these methods are re-bound to refer to the corresponding imple-
mentations in ListTable, which is the actual supertype of SynchronizedTable in
MyTable.
Syntax:
An object definition defines a single object of a new class. Its most general form is
object m extends t . Here, m is the name of the object to be defined, and t is a
template (§5.1) of the form
which defines the base classes, behavior and initial state of m. The extends
clause extends sc with mt 1 with . . . with mt n can be omitted, in which case
extends scala.AnyRef is assumed. The class body {stats } may also be omitted,
in which case the empty body {} is assumed.
The object definition defines a single object (or: module) conforming to the tem-
plate t . It is roughly equivalent to the following definition of a lazy value:
lazy val m = new sc with mt 1 with . . . with mt n { this: m.t y pe => stats }
Note that the value defined by an object definition is instantiated lazily. The
new m $cls constructor is evaluated not at the point of the object definition, but is
instead evaluated the first time m is dereferenced during execution of the program
(which might be never at all). An attempt to dereference m again in the course
of evaluation of the constructor leads to a infinite loop or run-time error. Other
threads trying to dereference m while the constructor is being evaluated block until
evaluation is complete.
The expansion given above is not accurate for top-level objects. It cannot be be-
cause variable and method definition cannot appear on the top-level outside of a
package object (§9.3). Instead, top-level objects are translated to static fields.
Example 5.4.1 Classes in Scala do not have static members; however, an equivalent
effect can be achieved by an accompanying object definition E.g.
val y: Double
def isOrigin = (x == 0.0 && y == 0.0)
}
object Point {
val origin = new Point() { val x = 0.0; val y = 0.0 }
}
This defines a class Point and an object Point which contains origin as a member.
Note that the double use of the name Point is legal, since the class definition defines
the name Point in the type name space, whereas the object definition defines a
name in the term namespace.
This technique is applied by the Scala compiler when interpreting a Java class with
static members. Such a class C is conceptually seen as a pair of a Scala class that
contains all instance members of C and a Scala object that contains all static mem-
bers of C .
Generally, a companion module of a class is an object which has the same name as
the class and is defined in the same scope and compilation unit. Conversely, the
class is called the companion class of the module.
Very much like a concrete class definition, an object definition may still contain
declarations of abstract type members, but not of abstract term members.
Chapter 6
Expressions
Syntax:
| SimpleExpr1 ArgumentExprs
| XmlExpr
Exprs ::= Expr {‘,’ Expr}
BlockExpr ::= ‘{’ CaseClauses ‘}’
| ‘{’ Block ‘}’
Block ::= {BlockStat semi} [ResultExpr]
ResultExpr ::= Expr1
| (Bindings | ([‘implicit’] id | ‘_’) ‘:’ CompoundType) ‘=>’ Bloc
Ascription ::= ‘:’ InfixType
| ‘:’ Annotation {Annotation}
| ‘:’ ‘_’ ‘*’
Expressions are composed of operators and operands. Expression forms are dis-
cussed subsequently in decreasing order of precedence.
The typing of expressions is often relative to some expected type (which might be
undefined). When we write “expression e is expected to conform to type T ”, we
mean: (1) the expected type of e is T , and (2) the type of expression e must conform
to T .
The following skolemization rule is applied universally for every expression: If the
type of an expression would be an existential type T , then the type of the expression
is assumed instead to be a skolemization (§3.2.10) of T .
Skolemization is reversed by type packing. Assume an expression e of type T and
let t 1 [tps1 ] >: L 1 <: U1 , . . . , t n [tpsn ] >: L n <: Un be all the type variables created by
skolemization of some part of e which are free in T . Then the packed type of e is
6.2 Literals
Syntax:
The null value is of type scala.Null, and is thus compatible with every reference
type. It denotes a reference value which refers to a special “null” object. This object
implements methods in class scala.AnyRef as follows:
• eq(x ) and ==(x ) return true iff the argument x is also the “null” object.
• ne(x ) and !=(x ) return true iff the argument x is not also the “null” object.
• isInstanceOf[T ] always returns false.
• asInstanceOf[T ] returns the default value (see §4.2) of type T .
• ## returns “0”.
6.4 Designators
Syntax:
1. The path p occurs as the prefix of a selection and it does not designate a con-
stant, or
76 Expressions
The selection e.x is evaluated by first evaluating the qualifier expression e, which
yields an object r , say. The selection’s result is then the member of r that is either
defined by m or defined by a definition overriding m. If that member has a type
which conforms to scala.NotNull, the member’s value must be initialized to a value
different from null, otherwise a scala.UnitializedError is thrown.
Syntax:
The expression this can appear in the statement part of a template or compound
type. It stands for the object being defined by the innermost template or compound
type enclosing the reference. If this is a compound type, the type of this is that
compound type. If it is a template of a class or object definition with simple name
C , the type of this is the same as the type of C .this.
The expression C .this is legal in the statement part of an enclosing class or object
definition with simple name C . It stands for the object being defined by the inner-
most such definition. If the expression’s expected type is a stable type, or C .this
occurs as the prefix of a selection, its type is C .this.type, otherwise it is the self
type of class C .
A reference super.m refers statically to a method or type m in the least proper
supertype of the innermost template containing the reference. It evaluates to the
member m 0 in the actual supertype of that template which is equal to m or which
overrides m. The statically referenced member m must be a type or a method. If
it is a method, it must be concrete, or the template containing the reference must
have a member m 0 which overrides m and which is labeled abstract override.
A reference C .super.m refers statically to a method or type m in the least proper
supertype of the innermost enclosing class or object definition named C which en-
closes the reference. It evaluates to the member m 0 in the actual supertype of that
class or object which is equal to m or which overrides m. The statically referenced
member m must be a type or a method. If the statically referenced member m is
a method, it must be concrete, or the innermost enclosing class or object defini-
tion named C must have a member m 0 which overrides m and which is labeled
abstract override.
6.6 Function Applications 77
The super prefix may be followed by a trait qualifier [T ], as in C .super[T ].x. This
is called a static super reference. In this case, the reference is to the type or method of
x in the parent trait of C whose simple name is T . That member must be uniquely
defined. If it is a method, it must be concrete.
Note that the superB function returns different results depending on whether B is
mixed in with class Root or A.
Syntax:
• For every named argument x i = e i0 the type S i is compatible with the parame-
ter type T j whose name p j matches x i .
• For every positional argument e i the type S i is compatible with Ti .
• If the expected type is defined, the result type U is compatible to it.
Example 6.6.1 Assume the following function which computes the sum of a vari-
able number of arguments:
6.6 Function Applications 79
Then
sum(1, 2, 3, 4)
sum(List(1, 2, 3, 4): _*)
sum(List(1, 2, 3, 4))
• The names x i of all named arguments are pairwise distinct and no named
argument defines a parameter which is already specified by a positional argu-
ment.
{ val q = p
q.m [targs]
}
{ val q = p
val x 1 = expr1
...
val x k = exprk
q.m [targs](args1 ), . . . , (argsl )
}
80 Expressions
{ val q = p
val x 1 = expr1
...
val x l = exprk
val y 1 = e 1
...
val y m = e m
val z 1 = q.m $default$i[targs](args1 ), . . . , (argsl )
...
val z d = q.m $default$j[targs](args1 ), . . . , (argsl )
q.m [targs](args1 ), . . . , (argsl )(args)
}
Syntax:
Example 6.7.1 The method values in the left column are each equivalent to the
anonymous functions (§6.23) on their right.
Note that a space is necessary between a method name and the trailing underscore
because otherwise the underscore would be considered part of the name.
Syntax:
6.9 Tuples
Syntax:
Syntax:
concrete self type is normally T , except if the expression new c appears as the right
hand side of a value definition
val x : S = new c
(where the type annotation : S may be missing). In the latter case, the concrete
self type of the expression is the compound type T with x .type.
The expression is evaluated by creating a fresh object of type T which is is initialized
by evaluating c. The type of the expression is T .
A general instance creation expression is of the form new t for some class template
t (§5.1). Such an expression is equivalent to the block
6.11 Blocks
Syntax:
The expected type of the final expression e is the expected type of the block. The
expected type of all preceding statements is undefined.
The type of a block s 1 ; . . .; s n ; e is T forSome { Q }, where T is the type of e
and Q contains existential clauses (§3.2.10) for every value or type name which is
free in T and which is defined locally in one of the statements s 1 , . . . , s n . We say the
existential clause binds the occurrence of the value or type name. Specifically,
simply has type B, because with the rules in (§3.2.10 the existentially quantified type
_1 forSome { type _1 <: B } can be simplified to B.
Syntax:
A prefix operation op e consists of a prefix operator op, which must be one of the
identifiers ‘+’, ‘-’, ‘!’ or ‘~’. The expression op e is equivalent to the postfix method
application e.unary_op.
Prefix operators are different from normal function applications in that their
operand expression need not be atomic. For instance, the input sequence -sin(x)
is read as -(sin(x)), whereas the function application negate sin(x) would be
parsed as the application of the infix operator sin to the operands negate and (x).
An infix operator can be an arbitrary identifier. Infix operators have precedence and
associativity defined as follows:
The precedence of an infix operator is determined by the operator’s first character.
Characters are listed below in increasing order of precedence, with characters on
the same line having the same precedence.
(all letters)
|
^
&
< >
= !
:
+ -
* / %
(all other special characters)
That is, operators starting with a letter have lowest precedence, followed by opera-
tors starting with ‘|’, etc.
There’s one exception to this rule, which concerns assignment operators(§6.12.4).
The precedence of an assigment operator is the same as the one of simple assign-
ment (=). That is, it is lower than the precedence of any other operator.
The associativity of an operator is determined by the operator’s last character. Op-
erators ending in a colon ‘:’ are right-associative. All other operators are left-
associative.
Precedence and associativity of operators determine the grouping of parts of an ex-
pression as follows.
6.12 Prefix, Infix, and Postfix Operations 85
Assignment operators are treated specially in that they can be expanded to assign-
ments if no other interpretation is valid.
Let’s consider an assignment operator such as += in an infix operation l += r ,
where l , r are expressions. This operation can be re-interpreted as an operation
which corresponds to the assignment
l = l + r
1. The left-hand-side l does not have a member named +=, and also cannot be
converted by an implicit conversion (§6.26) to a value with a member named
+=.
Syntax:
The typed expression e : T has type T . The type of expression e is expected to con-
form to T . The result of the expression is the value of e converted to type T .
Example 6.13.1 Here are examples of well-typed and illegally typed expressions.
Syntax:
6.15 Assignments
Syntax:
Example 6.15.1 Here are some assignment expressions and their equivalent ex-
pansions.
x.f = e x.f_=(e)
x.f() = e x.f.update(e)
x.f(i) = e x.f.update(i, e)
x.f(i, j) = e x.f.update(i, j, e)
Example 6.15.2 Here is the usual imperative code for matrix multiplication.
Desugaring the array accesses and assignments yields the following expanded ver-
sion:
}
i += 1
}
zss
}
Syntax:
Expr1 ::= ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] ‘else’ Expr]
Syntax:
Syntax:
6.19 For Comprehensions and For Loops 89
Syntax:
A for loop for (enums ) e executes expression e for each binding generated by
the enumerators enums. A for comprehension for (enums ) yield e evaluates
expression e for each binding generated by the enumerators enums and collects
the results. An enumerator sequence always starts with a generator; this can be fol-
lowed by further generators, value definitions, or guards. A generator p <- e pro-
duces bindings from an expression e which is matched in some way against pat-
tern p. A value definition p = e binds the value name p (or several names in a
pattern p) to the result of evaluating the expression e. A guard if e contains a
boolean expression which restricts enumerated bindings. The precise meaning of
generators and guards is defined by translation to invocations of four methods: map,
withFilter, flatMap, and foreach. These methods can be implemented in different
ways for different carrier types.
The translation scheme is as follows. In a first step, every generator p <- e, where
p is not irrefutable (§8.1) for the type of e is replaced by
Then, the following rules are applied repeatedly until all comprehensions have been
eliminated.
• A for loop
Example 6.19.1 The following code produces all pairs of numbers between 1 and
n − 1 whose sums are prime.
(1 until n)
.flatMap {
case i => (1 until i)
.withFilter { j => isPrime(i+j) }
.map { case j => (i, j) } }
Example 6.19.2 For comprehensions can be used to express vector and matrix al-
gorithms concisely. For instance, here is a function to compute the transpose of a
given matrix:
Finally, here is a function to compute the product of two matrices. Compare with
the imperative version of Example 6.15.2.
The code above makes use of the fact that map, flatMap, withFilter, and foreach
are defined for instances of class scala.Array.
Syntax:
A return expression return e must occur inside the body of some enclosing named
method or function. The innermost enclosing named method or function in a
source program, f , must have an explicitly declared result type, and the type of e
must conform to it. The return expression evaluates the expression e and returns its
value as the result of f . The evaluation of any statements or expressions following
the return expression is omitted. The type of a return expression is scala.Nothing.
The expression e may be omitted. The return expression return is type-checked
and evaluated as if it was return ().
An apply method which is generated by the compiler as an expansion of an anony-
mous function does not count as a named function in the source program, and
therefore is never the target of a return expression.
Returning from a nested anonymous function is implemented by throwing and
catching a scala.runtime.NonLocalReturnException. Any exception catches be-
tween the point of return and the enclosing methods might see the exception. A
92 Expressions
key comparison makes sure that these exceptions are only caught by the method
instance which is terminated by the return.
If the return expression is itself part of an anonymous function, it is possible that
the enclosing instance of f has already returned before the return expression is ex-
ecuted. In that case, the thrown scala.runtime.NonLocalReturnException will not
be caught, and will propagate up the call stack.
Syntax:
A throw expression throw e evaluates the expression e. The type of this expression
must conform to Throwable. If e evaluates to an exception reference, evaluation
is aborted with the thrown exception. If e evaluates to null, evaluation is instead
aborted with a NullPointerException. If there is an active try expression (§6.22)
which handles the thrown exception, evaluation resumes with the handler; other-
wise the thread executing the throw is aborted. The type of a throw expression is
scala.Nothing.
Syntax:
Expr1 ::= ‘try’ ‘{’ Block ‘}’ [‘catch’ ‘{’ CaseClauses ‘}’]
[‘finally’ Expr]
A try expression is of the form try { b } catch h where the handler h is a pattern
matching anonymous function (§8.5)
Syntax:
Syntax:
An expression (of syntactic category Expr) may contain embedded underscore sym-
bols _ at places where identifiers are legal. Such an expression represents an anony-
mous function where subsequent occurrences of underscores denote successive
parameters.
Define an underscore section to be an expression of the form _:T where T is a type,
or else of the form _, provided the underscore does not appear as the expression
part of a type ascription _:T .
An expression e of syntactic category Expr binds an underscore section u, if the fol-
lowing two conditions hold: (1) e properly contains u, and (2) there is no other ex-
pression of syntactic category Expr which is properly contained in e and which itself
properly contains u.
If an expression e binds underscore sections u 1 , . . . , u n , in this order, it is equivalent
to the anonymous function (u 10 , ... u n0 ) => e 0 where each u i0 results from u i by
replacing the underscore with a fresh identifier and e 0 results from e by replacing
6.24 Constant Expressions 95
Example 6.23.2 The anonymous functions in the left column use placeholder syn-
tax. Each of these is equivalent to the anonymous function on its right.
_ + 1 x => x + 1
_ * _ (x1, x2) => x1 * x2
(_: Int) * 2 (x: Int) => (x: Int) * 2
if (_) x else y z => if (z) x else y
_.map(f) x => x.map(f)
_.map(_ + 1) x => x.map(y => y + 1)
Constant expressions are expressions that the Scala compiler can evaluate to a con-
stant. The definition of “constant expression” depends on the platform, but they
include at least the expressions of the following forms:
6.25 Statements
Syntax:
Implicit conversions can be applied to expressions whose type does not match their
expected type, to qualifiers in selections, and to unapplied methods. The available
implicit conversions are given in the next two sub-sections.
We say, a type T is compatible to a type U if T weakly conforms to U after applying
eta-expansion (§6.26.5) and view applications (§7.3).
The following five implicit conversions can be applied to an expression e which has
some value type T and which is type-checked with some expected type pt.
which does not appear as the function part of a type application is converted to a
type instance of T by determining with local type inference (§6.26.4) instance types
T1 , . . . , Tn for the type variables a 1 , . . . , a n and implicitly embedding e in the type
application e [T1 , . . . , Tn ] (§6.8).
Numeric Literal Narrowing. If the expected type is Byte, Short or Char, and the
expression e is an integer literal fitting in the range of that type, it is converted to the
same literal in that type.
Value Discarding. If e has some value type and the expected type is Unit, e is con-
verted to the expected type by embedding it in the term { e ; () }.
View Application. If none of the previous conversions applies, and e’s type does
not conform to the expected type pt, it is attempted to convert e to the expected
type with a view (§7.3).
The following four implicit conversions can be applied to methods which are not
applied to some argument list.
Implicit Application. If the method takes only implicit parameters, implicit argu-
ments are passed following the rules of §7.2.
Eta Expansion. Otherwise, if the method is not a constructor, and the expected
type pt is a function type (Ts0 ) ⇒ T 0 , eta-expansion (§6.26.5) is performed on the
expression e.
One first determines the set of functions that is potentially applicable based on the
shape of the arguments.
The shape of an argument expression e, written shape(e), is a type that is defined as
follows:
A class or object C is derived from a class or object D if one of the following holds:
• C is a subclass of D, or
• C is a companion object of a class derived from D, or
• D is a companion object of a class from which C is derived.
class A extends B {}
def f(x: B, y: B) = . . .
def f(x: A, y: B) = . . .
val a: A
val b: B
Then the application f(b, b) refers to the first definition of f whereas the applica-
tion f(a, a) refers to the second. Assume now we add a third overloaded definition
def f(x: B, y: A) = . . .
Then the application f(a, a) is rejected for being ambiguous, since no most spe-
cific applicable signature exists.
100 Expressions
Case 3: Methods. The last case applies if the expression e appears in an applica-
tion e(d 1 , . . . , d m ). In that case T is a method type (p 1 : R 1 , . . . , p m : R m )T 0 . With-
out loss of generality we can assume that the result type T 0 is a value type; if it is a
method type we apply eta-expansion (§6.26.5) to convert it to a function type. One
computes first the types S j of the argument expressions d j , using two alternative
schemes. Each argument expression d j is typed first with the expected type R j , in
which the type parameters a 1 , . . . , a n are taken as type constants. If this fails, the
6.26 Implicit Conversions 101
It is possible that no minimal or maximal solution for a type variable exists, in which
case a compile-time error results. Because <: is a pre-order, it is also possible that a
solution set has several optimal solutions for a type. In that case, a Scala compiler is
free to pick any one of them.
The application of cons is typed with an undefined expected type. This application
is completed by local type inference to cons[Int](1, nil). Here, one uses the
following reasoning to infer the type argument Int for the type parameter a:
First, the argument expressions are typed. The first argument 1 has type Int
whereas the second argument nil is itself polymorphic. One tries to type-check
nil with an expected type List[a]. This leads to the constraint system
b = scala.Nothing .
In a second step, one solves the following constraint system for the type parameter
a of cons:
Int <: a?
List[scala.Nothing] <: List[a?]
List[a?] <: undefined
a = Int ,
where xs is defined of type List[Int] as before. In this case local type inference
proceeds as follows.
First, the argument expressions are typed. The first argument "abc" has type
String. The second argument xs is first tried to be typed with expected type
List[a]. This fails, as List[Int] is not a subtype of List[a]. Therefore, the sec-
ond strategy is tried; xs is now typed with expected type List[undefined]. This
succeeds and yields the argument type List[Int].
In a second step, one solves the following constraint system for the type parameter
a of cons:
String <: a?
List[Int] <: List[a?]
List[a?] <: undefined
a = scala.Any ,
First, one identifes the maximal sub-expressions of e; let’s say these are e 1 , . . . , e m .
For each of these, one creates a fresh name x i . Let e 0 be the expression resulting
from replacing every maximal subexpression e i in e by the corresponding fresh
name x i . Second, one creates a fresh name y i for every argument type Ti of the
method (i = 1, . . . , n). The result of eta-conversion is then:
{ val x 1 = e 1 ;
...
val x m = e m ;
( y 1 : T1 , . . . , y n : Tn ) => e 0 ( y 1 , . . . , y n )
}
The standard Scala library defines a trait scala.Dynamic which defines a member
invokeDynamic@ as follows:
package scala
trait Dynamic {
def applyDynamic (name: String, args: Any*): Any
...
}
Assume a selection of the form e.x where the type of e conforms to scala.Dynamic.
Further assuming the selection is not followed by any function arguments, such an
expression can be rewitten under the conditions given in §6.26 to:
e .applyDynamic("x ")
If the selection is followed by some arguments, e.g. e.x(args), then that expression
is rewritten to
Syntax:
Example 7.1.1 The following code defines an abstract class of monoids and two
concrete implementations, StringMonoid and IntMonoid. The two implementa-
tions are marked implicit.
If there are several eligible arguments which match the implicit parameter’s type,
a most specific one will be chosen using the rules of static overloading resolution
(§6.26.3). If the parameter has a default argument and no implicit argument can be
found the default argument is used.
Example 7.2.1 Assuming the classes from Example 7.1.1, here is a method which
computes the sum of a list of elements using the monoid’s add and unit operations.
sum(List(1, 2, 3))
in a context where stringMonoid and intMonoid are visible. We know that the formal
type parameter a of sum needs to be instantiated to Int. The only eligible object
which matches the implicit formal parameter type Monoid[Int] is intMonoid so this
object will be passed as implicit parameter.
This discussion also shows that implicit parameters are inferred after any type ar-
guments are inferred (§6.26.4).
Implicit methods can themselves have implicit parameters. An example is the fol-
lowing method from module scala.List, which injects lists into the scala.Ordered
class, provided the element type of the list is also convertible to this type.
that injects integers into the Ordered class. We can now define a sort method over
ordered lists:
sort(yss)
The call above will be completed by passing two nested implicit arguments:
The possibility of passing implicit arguments to implicit arguments raises the pos-
sibility of an infinite recursion. For instance, one might try to define the following
method, which injects every type into the Ordered class:
Now, if one tried to apply sort to an argument arg of a type that did not have an-
other injection into the Ordered class, one would obtain an infinite expansion:
To prevent such infinite expansions, the compiler keeps track of a stack of “open
implicit types” for which implicit arguments are currently being searched. When-
ever an implicit argument for type T is searched, the “core type” of T is added to the
stack. Here, the core type of T is T with aliases expanded, top-level type annotations
(§11) and refinements (§3.2.7) removed, and occurrences of top-level existentially
bound variables replaced by their upper bounds. The core type is removed from the
stack once the search for the implicit argument either definitely fails or succeeds.
Everytime a core type is added to the stack, it is checked that this type does not
dominate any of the other types in the set.
Here, a core type T dominates a type U if T is equivalent (§3.5.1) to U , or if the top-
level type constructors of T and U have a common element and T is more complex
than U .
The set of top-level type constructors ttcs(T ) of a type T depends on the form of the
type:
The complexity complexity(T ) of a core type is an integer which also depends on the
form of the type:
All types share the common type constructor scala.Function1, but the complexity
of the each new type is lower than the complexity of the previous types. Hence, the
code typechecks.
Example 7.2.3 Let ys be a list of some type which cannot be converted to Ordered.
For instance:
Assume that the definition of magic above is in scope. Then the sequence of types
for which implicit arguments are searched is
Since the second type in the sequence is equal to the first, the compiler will issue an
error signalling a divergent implicit expansion.
7.3 Views
Implicit parameters and methods can also define implicit conversions called views.
A view from type S to type T is defined by an implicit value which has function type
S =>T or (=>S )=>T or by a method convertible to a value of that type.
Views are applied in three situations.
2. In a selection e.m with e of type T , if the selector m does not denote an acces-
sible member of T . In this case, a view v is searched which is applicable to e
and whose result contains a member named m. The search proceeds as in the
case of implicit parameters, where the implicit scope is the one of T . If such a
view is found, the selection e.m is converted to v (e ).m.
Assume two lists xs and ys of type List[Int] and assume that the list2ordered
and int2ordered methods defined in §7.2 are in scope. Then the operation
xs <= ys
list2ordered(xs)(int2ordered).<=
(ys)
(xs => list2ordered(xs)(int2ordered))
Syntax:
A type parameter A of a method or non-trait class may have one or more view
bounds A <% T . In this case the type parameter may be instantiated to any type
S which is convertible by application of a view to the bound T .
A type parameter A of a method or non-trait class may also have one or more con-
text bounds A : T . In this case the type parameter may be instantiated to any type
S for which evidence exists at the instantiation point that S satisfies the bound T .
Such evidence consists of an implicit value with type T [S].
A method or class containing type parameters with view or context bounds is
treated as being equivalent to a method with implicit parameters. Consider first
the case of a single parameter with view and/or context bounds such as:
where the v i and w j are fresh names for the newly introduced implicit parameters.
These parameters are called evidence parameters.
If a class or method has several view- or context-bounded type parameters, each
such type parameter is expanded into evidence parameters in the order they appear
and all the resulting evidence parameters are concatenated in one implicit param-
eter section. Since traits do not take constructor parameters, this translation does
not work for them. Consequently, type-parameters in traits may not be view- or
context-bounded. Also, a method or class with view- or context bounds may not
define any additional implicit parameters.
Example 7.4.1 The <= method mentioned in Example 7.3.1 can be declared more
concisely as follows:
7.5 Manifests
Manifests are type descriptors that can be automatically generated by the Scala
compiler as arguments to implicit parameters. The Scala standard library contains
a hierarchy of four manifest classes, with OptManifest at the top. Their signatures
follow the outline below.
trait OptManifest[+T]
object NoManifest extends OptManifest[Nothing]
trait ClassManifest[T] extends OptManifest[T]
trait Manifest[T] extends ClassManifest[T]
1. If T is a value class or one of the classes Any, AnyVal, Object, Null, or Nothing,
a manifest for it is generated by selecting the corresponding manifest value
Manifest.T , which exists in the Manifest module.
112 Implicit Parameters and Views
Pattern Matching
8.1 Patterns
Syntax:
A pattern is built from constants, constructors, variables and type tests. Pattern
matching tests whether a given value (or sequence of values) has the shape defined
by a pattern, and, if it does, binds the variables in the pattern to the corresponding
components of the value (or sequence of values). The same variable name may not
be bound more than once in a pattern.
Pattern matching is always done in a context which supplies an expected type of the
pattern. We distinguish the following kinds of patterns.
Syntax:
A variable pattern x is a simple identifier which starts with a lower case letter. It
matches any value, and binds the variable name to that value. The type of x is the
expected type of the pattern as given from outside. A special case is the wild-card
pattern _ which is treated as if it was a fresh variable on each occurrence.
Syntax:
A typed pattern x : T consists of a pattern variable x and a type pattern T . The type
of x is the type pattern T , where each type variable and wildcard is replaced by a
fresh, unknown type. This pattern matches any value matched by the type pattern
T (§8.2); it binds the variable name to that value.
Syntax:
Syntax:
A literal pattern L matches any value that is equal (in terms of ==) to the literal L.
The type of L must conform to the expected type of the pattern.
Syntax:
A stable identifier pattern is a stable identifier r (§3.1). The type of r must conform
to the expected type of the pattern. The pattern matches any value v such that
r == v (§12.1).
To resolve the syntactic overlap with a variable pattern, a stable identifier pattern
may not be a simple name starting with a lower-case letter. However, it is possible to
enclose a such a variable name in backquotes; then it is treated as a stable identifier
pattern.
Here, y is a variable pattern, which matches any value. If we wanted to turn the
pattern into a stable identifier pattern, this can be achieved as follows:
Now, the pattern matches the y parameter of the enclosing function f. That is, the
match succeeds only if the x argument and the y argument of f are equal.
Syntax:
or qualified name which denotes a case class (§5.3.2). If the case class is monomor-
phic, then it must conform to the expected type of the pattern, and the formal pa-
rameter types of x’s primary constructor (§5.3) are taken as the expected types of the
element patterns p 1 , . . . , p n . If the case class is polymorphic, then its type parame-
ters are instantiated so that the instantiation of c conforms to the expected type of
the pattern. The instantiated formal parameter types of c’s primary constructor are
then taken as the expected types of the component patterns p 1 , . . . , p n . The pattern
matches all objects created from constructor invocations c(v 1 , . . . , v n ) where each
element pattern p i matches the corresponding value v i .
A special case arises when c’s formal parameter types end in a repeated parameter.
This is further discussed in (§8.1.9).
Syntax:
Syntax:
n = 0 and unapply’s result type is Boolean. In this case the extractor pattern
matches all values v for which x .unapply(v ) yields true.
n = 1 and unapply’s result type is Option[T ], for some type T . In this case,
the (only) argument pattern p 1 is typed in turn with expected type T . The
extractor pattern matches then all values v for which x .unapply(v ) yields a
value of form Some(v 1 ), and p 1 matches v 1 .
n > 1 and unapply’s result type is Option[(T1 , . . . , Tn )], for some types
T1 , . . . , Tn . In this case, the argument patterns p 1 , . . . , p n are typed in turn
with expected types T1 , . . . , Tn . The extractor pattern matches then all values
8.1 Patterns 117
v for which x .unapply(v ) yields a value of form Some((v 1 , . . . , v n )), and each
pattern p i matches the corresponding value v i .
Example 8.1.3 The Predef object contains a definition of an extractor object Pair:
object Pair {
def apply[A, B](x: A, y: B) = Tuple2(x, y)
def unapply[A, B](x: Tuple2[A, B]): Option[Tuple2[A, B]] = Some(x)
}
This means that the name Pair can be used in place of Tuple2 for tuple formation
as well as for deconstruction of tuples in patterns. Hence, the following is possible:
val x = (1, 2)
val y = x match {
case Pair(i, s) => Pair(s + i, i * i)
}
Syntax:
SimplePattern ::= StableId ‘(’ [Patterns ‘,’] [varid ‘@’] ‘_’ ‘*’ ‘)’
Syntax:
118 Pattern Matching
Syntax:
Regular expression patterns have been discontinued in Scala from version 2.0.
Later version of Scala provide a much simplified version of regular expression pat-
terns that cover most scenarios of non-text sequence processing. A sequence pattern
is a pattern that stands in a position where either (1) a pattern of a type T which is
conforming to Seq[A] for some A is expected, or (2) a case class constructor that has
an iterated formal parameter A*. A wildcard star pattern _* in the rightmost position
stands for arbitrary long sequences. It can be bound to variables using @, as usual,
in which case the variable will have the type Seq[A].
1. p is a variable pattern,
Syntax:
Type patterns consist of types, type variables, and wildcards. A type pattern T is of
one of the following forms:
• A singleton type p .type. This type pattern matches only the value denoted
by the path p (that is, a pattern match involved a comparison of the matched
value with p using method eq in class AnyRef).
Types which are not of one of the forms described above are also accepted as type
patterns. However, such type patterns will be translated to their erasure (§3.7). The
Scala compiler will issue an “unchecked” warning for these patterns to flag the pos-
sible loss of type-safety.
A type variable pattern is a simple identifier which starts with a lower case letter.
Type parameter inference is the process of finding bounds for the bound type vari-
ables in a typed pattern or constructor pattern. Inference takes into account the
expected type of the pattern.
120 Pattern Matching
Type parameter inference for typed patterns.. Assume a typed pattern p : T 0 . Let
T result from T 0 where all wildcards in T 0 are renamed to fresh variable names. Let
a 1 , . . . , a n be the type variables in T . These type variables are considered bound in
the pattern. Let the expected type of the pattern be pt.
Type parameter inference constructs first a set of subtype constraints over the type
variables a i . The initial constraints set C 0 reflects just the bounds of these type vari-
ables. That is, assuming T has bound type variables a 1 , . . . , a n which correspond
to class type parameters a 10 , . . . , a n0 with lower bounds L 1 , . . . , L n and upper bounds
U1 , . . . , Un , C 0 contains the constraints
ai <: σUi (i = 1, . . . , n)
σL i <: a i (i = 1, . . . , n)
Case 1:. If there exists a substitution σ over the type variables a i , . . . , a n such that
σT conforms to pt, one determines the weakest subtype constraints C 1 over the
type variables a 1 , . . . , a n such that C 0 ∧ C 1 implies that T conforms to pt.
Case 2:. Otherwise, if T can not be made to conform to pt by instantiating its type
variables, one determines all type variables in pt which are defined as type param-
eters of a method enclosing the pattern. Let the set of such type parameters be
b 1 , . . . , b m . Let C 00 be the subtype constraints reflecting the bounds of the type vari-
ables b i . If T denotes an instance type of a final class, let C 2 be the weakest set
of subtype constraints over the type variables a 1 , . . . , a n and b 1 , . . . , b m such that
C 0 ∧ C 00 ∧ C 2 implies that T conforms to pt. If T does not denote an instance type
of a final class, let C 2 be the weakest set of subtype constraints over the type vari-
ables a 1 , . . . , a n and b 1 , . . . , b m such that C 0 ∧ C 00 ∧ C 2 implies that it is possible to
construct a type T 0 which conforms to both T and pt. It is a static error if there is no
satisfiable set of constraints C 2 with this property.
The final step consists in choosing type bounds for the type variables which imply
the established constraint system. The process is different for the two cases above.
Case 1:. We take a i >: L i <: Ui where each L i is minimal and each Ui is maximal
wrt <: such that a i >: L i <: Ui for i = 1, . . . , n implies C 0 ∧ C 1 .
Case 2:. We take a i >: L i <: Ui and b i >: L 0i <: Ui0 where each L i and L 0j is minimal
and each Ui and U j0 is maximal such that a i >: L i <: Ui for i = 1, . . . , n and b j >:
L 0j <: U j0 for j = 1, . . . , m implies C 0 ∧ C 00 ∧ C 2 .
8.3 Type Parameter Inference in Patterns 121
In both cases, local type inference is permitted to limit the complexity of inferred
bounds. Minimality and maximality of types have to be understood relative to the
set of types of acceptable complexity.
val x: Any
x match {
case y: List[a] => ...
}
Here, the type pattern List[a] is matched against the expected type Any. The pat-
tern binds the type variable a. Since List[a] conforms to Any for every type argu-
ment, there are no constraints on a. Hence, a is introduced as an abstract type with
no bounds. The scope of a is right-hand side of its case clause.
On the other hand, if x is declared as
val x: List[List[String]],
val x: Any
x match {
case y: List[String] => ...
}
Scala does not maintain information about type arguments at run-time, so there is
no way to check that x is a list of strings. Instead, the Scala compiler will erase (§3.7)
the pattern to List[_]; that is, it will only test whether the top-level runtime-class
of the value x conforms to List, and the pattern match will succeed if it does. This
might lead to a class cast exception later on, in the case where the list x contains
elements other than strings. The Scala compiler will flag this potential loss of type-
safety with an “unchecked” warning message.
class Term[A]
122 Pattern Matching
The expected type of the pattern y: Number is Term[B]. The type Number does not
conform to Term[B]; hence Case 2 of the rules above applies. This means that b
is treated as another type variable for which subtype constraints are inferred. In
our case the applicable constraint is Number <: Term[B], which entails B = Int.
Hence, B is treated in the case clause as an abstract type with lower and upper bound
Int. Therefore, the right hand side of the case clause, y.n, of type Int, is found to
conform to the function’s declared result type, Number.
Syntax:
consists of a selector expression e and a number n > 0 of cases. Each case consists
of a (possibly guarded) pattern p i and a block b i . Each p i might be complemented
by a guard if e where e is a boolean expression. The scope of the pattern variables
in p i comprises the pattern’s guard and the corresponding block b i .
Let T be the type of the selector expression e and let a 1 , . . . , a m be the type param-
eters of all methods enclosing the pattern matching expression. For every a i , let L i
be its lower bound and Ui be its higher bound. Every pattern p ∈ {p 1 , , . . . , p n } can
be typed in two ways. First, it is attempted to type p with T as its expected type. If
this fails, p is instead typed with a modified expected type T 0 which results from T
by replacing every occurrence of a type parameter a i by undefined. If this second
step fails also, a compile-time error results. If the second step succeeds, let T p be
the type of pattern p seen as an expression. One then determines minimal bounds
L 01 , . . . , L 0m and maximal bounds U10 , . . . , Um
0
such that for all i , L i <: L 0i and Ui0 <: Ui
and the following constraint system is satisfied:
If no such bounds can be found, a compile time error results. If such bounds are
found, the pattern matching clause starting with p is then typed under the assump-
8.4 Pattern Matching Expressions 123
tion that each a i has lower bound L 0i instead of L i and has upper bound Ui0 instead
of Ui .
The expected type of every block b i is the expected type of the whole pattern match-
ing expression. The type of the pattern matching expression is then the weak least
upper bound (§3.5.3) of the types of all blocks b i .
When applying a pattern matching expression to a selector value, patterns are tried
in sequence until one is found which matches the selector value (§8.1). Say this case
is case p i ⇒ b i . The result of the whole expression is then the result of evaluating b i ,
where all pattern variables of p i are bound to the corresponding parts of the selector
value. If no matching pattern is found, a scala.MatchError exception is thrown.
The pattern in a case may also be followed by a guard suffix if e with a boolean
expression e. The guard expression is evaluated if the preceding pattern in the case
matches. If the guard expression evaluates to true, the pattern match succeeds as
normal. If the guard expression evaluates to false, the pattern in the case is con-
sidered not to match and the search for a matching pattern continues.
In the interest of efficiency the evaluation of a pattern matching expression may try
patterns in some other order than textual sequence. This might affect evaluation
through side effects in guards. However, it is guaranteed that a guard expression is
evaluated only if the pattern it guards matches.
If the selector of a pattern match is an instance of a sealed class (§5.2), the com-
pilation of pattern matching can emit warnings which diagnose that a given set of
patterns is not exhaustive, i.e. that there is a possibility of a MatchError being raised
at run-time.
There are terms to represent numeric literals, incrementation, a zero test, and a
conditional. Every term carries as a type parameter the type of the expression it
representes (either Int or Boolean).
A type-safe evaluator for such terms can be written as follows.
Note that the evaluator makes crucial use of the fact that type parameters of enclos-
ing methods can acquire new bounds through pattern matching.
For instance, the type of the pattern in the second case, Succ(u), is Int. It conforms
to the selector type T only if we assume an upper and lower bound of Int for T.
Under the assumption Int <: T <: Int we can also verify that the type right hand
side of the second case, Int conforms to its expected type, T.
Syntax:
(x 1 : S 1 , . . . , x k : S k ) => (x 1 , . . . , x k ) match {
case p 1 => b 1 . . . case p n => b n
}
Here, each x i is a fresh name. As was shown in (§6.23), this anonymous function
is in turn equivalent to the following instance creation expression, where T is the
weak least upper bound of the types of all b i .
new scala.Functionk [S 1 , . . . , S k , T ] {
def apply(x 1 : S 1 , . . . , x k : S k ): T = (x 1 , . . . , x k ) match {
case p 1 => b 1 . . . case p n => b n
}
}
new scala.PartialFunction[S , T ] {
8.5 Pattern Matching Anonymous Functions 125
Here, x is a fresh name and T is the weak least upper bound of the types of all b i .
The final default case in the isDefinedAt method is omitted if one of the patterns
p 1 , . . . , p n is already a variable or wildcard pattern.
Example 8.5.1 Here is a method which uses a fold-left operation /: to compute the
scalar product of two vectors:
The case clauses in this code are equivalent to the following anonymous funciton:
Top-Level Definitions
Syntax:
package p 1 ;
...
package p n ;
stats
starting with one or more package clauses is equivalent to a compilation unit con-
sisting of the packaging
package p 1 { . . .
package p n {
stats
} ...
}
128 Top-Level Definitions
Implicitly imported into every compilation unit are, in that order : the package
java.lang, the package scala, and the object scala.Predef (§12.5). Members of
a later import in that order hide members of an earlier import.
9.2 Packagings
Syntax:
A package is a special object which defines a set of member classes, objects and
packages. Unlike other objects, packages are not introduced by a definition. In-
stead, the set of members of a package is determined by packagings.
A packaging package p { ds } injects all definitions in ds as members into the
package whose qualified name is p. Members of a package are called top-level def-
initions. If a definition in ds is labeled private, it is visible only for other members
in the package.
Inside the packaging, all members of package p are visible under their simple
names. However this rule does not extend to members of enclosing packages of
p that are designated by a prefix of the path p.
package org.net.prj {
...
}
all members of package org.net.prj are visible under their simple names, but
members of packages org or org.net require explicit qualification or imports.
Selections p.m from p as well as imports from p work as for objects. However, unlike
other objects, packages may not be used as values. It is illegal to have a package with
the same fully qualified name as a module or a class.
Top-level definitions outside a packaging are assumed to be injected into a special
empty package. That package cannot be named and therefore cannot be imported.
However, members of the empty package are visible to each other without qualifi-
cation.
Syntax:
Syntax:
A reference to a package takes the form of a qualified identifier. Like all other ref-
erences, package references are relative. That is, a package reference starting in
a name p will be looked up in the closest enclosing scope that defines a member
named p.
The special predefined name _root_ refers to the outermost root package which
contains all top-level packages.
package b {
class B
}
package a.b {
class A {
val x = new _root_.b.B
}
}
Here, the reference _root_.b.B refers to class B in the toplevel package b. If the
_root_ prefix had been omitted, the name b would instead resolve to the package
a.b, and, provided that package does not also contain a class B, a compiler-time
error would result.
9.5 Programs
gram’s command arguments are are passed to the main method as a parameter of
type Array[String].
The main method of a program can be directly defined in the object, or it can be
inherited. The scala library defines a special class scala.App whose body acts as
a ‘main‘ method. An object m inheriting from this class is thus a program, which
executes the initializaton code of the object m.
Example 9.5.1 The following example will create a hello world program by defining
a method main in module test.HelloWorld.
package test
object HelloWord {
def main(args: Array[String]) { println("hello world") }
}
scala test.HelloWorld
java test.HelloWorld
package test
object HelloWord extends App {
println("hello world")
}
Chapter 10
By Burak Emir
This chapter describes the syntactic structure of XML expressions and patterns. It
follows as closely as possible the XML 1.0 specification [W3C], changes being man-
dated by the possibility of embedding Scala code fragments.
XML expressions are expressions generated by the following production, where the
opening bracket ‘<’ of the first element must be in a position to start the lexical XML
mode (§1.5).
Syntax:
Well-formedness constraints of the XML specification apply, which means for in-
stance that start tags and end tags must match, and attributes may only be defined
once, with the exception of constraints related to entity resolution.
The following productions describe Scala’s extensible markup language, designed
as close as possible to the W3C extensible markup language standard. Only the pro-
ductions for attribute values and character data are changed. Scala does not sup-
port declarations, CDATA sections or processing instructions. Entity references are
not resolved at runtime.
Syntax:
XML expressions may contain Scala expressions as attribute values or within nodes.
In the latter case, these are embedded using a single opening brace ‘{’ and ended by
a closing brace ‘}’. To express a single opening braces within XML text as generated
by CharData, it must be doubled. Thus, ‘{{’ represents the XML text ‘{’ and does not
introduce an embedded Scala expression.
Syntax:
10.2 XML patterns 133
XML patterns are patterns generated by the following production, where the open-
ing bracket ‘<’ of the element patterns must be in a position to start the lexical XML
mode (§1.5).
Syntax:
User-Defined Annotations
Syntax:
@transient
@volatile
Marks a field which can change its value outside the control of the pro-
gram; this is equivalent to the volatile modifier in Java.
@SerialVersionUID(<longlit>)
@throws[<classlit>]
@scala.beans.BeanProperty
@scala.beans.BooleanBeanProperty
@deprecatedName(name: <symbollit>)
@unchecked
@uncheckedStable
type A { type T }
type B
@uncheckedStable val x: A with B // volatile type
val y: x.T // OK since ‘x’ is still a path
@specialized
The Scala standard library consists of the package scala with a number of classes
and modules. Some of these classes are described in the following.
Figure 12.1 illustrates Scala’s class hierarchy. The root of this hierarchy is formed by
class Any. Every class in a Scala execution environment inherits directly or indirectly
from this class. Class Any has two direct subclasses: AnyRef and AnyVal.
The subclass AnyRef represents all values which are represented as objects in
the underlying host system. Classes written in other languages inherit from
scala.AnyRef.
The predefined subclasses of class AnyVal describe values which are not imple-
mented as objects in the underlying host system.
User-defined Scala classes which do not explicitly inherit from ‘AnyVal‘ inherit di-
rectly or indirectly from ‘AnyRef‘. They can not inherit from both ‘AnyRef‘ and ‘Any-
Val‘.
Classes AnyRef and AnyVal are required to provide only the members declared in
class Any, but implementations may add host-specific methods to these classes (for
instance, an implementation may identify class AnyRef with its own root class for
objects).
The signatures of these root classes are described by the following definitions.
package scala
/** The universal root class */
abstract class Any {
x match {
case _: T 0 => true
case _ => false
}
where the type T 0 is the same as T except if T is of the form D or D[tps] where
D is a type member of some outer class C . In this case T 0 is C #D (or C #D[t ps],
respectively), whereas T itself would expand to C .this.D[t ps]. In other words, an
isInstanceOf test does not check for the
Value classes are classes whose instances are not represented as objects by the un-
derlying host system. All value classes inherit from class AnyVal. Scala implemen-
tations need to provide the value classes Unit, Boolean, Double, Float, Long, Int,
Char, Short, and Byte (but are free to provide others as well). The signatures of
these classes are defined in the following.
142 The Scala Standard Library
Classes Double, Float, Long, Int, Char, Short, and Byte are together called numeric
value types. Classes Byte, Short, or Char are called subrange types. Subrange types,
as well as Int and Long are called integer types, whereas Float and Double are called
floating point types.
Numeric value types are ranked in the following partial order:
Byte - Short
\
Int - Long - Float - Double
/
Char
Byte and Short are the lowest-ranked types in this order, whereas Double is the
highest-ranked. Ranking does not imply a conformance (§3.5.2) relationship; for
instance Int is not a subtype of Long. However, object Predef (§12.5) defines views
(§7.3) from every numeric value type to all higher-ranked numeric value types.
Therefore, lower-ranked types are implicitly converted to higher-ranked types when
required by the context (§6.26).
Given two numeric value types S and T , the operation type of S and T is defined as
follows: If both S and T are subrange types then the operation type of S and T is Int.
Otherwise the operation type of S and T is the larger of the two types wrt ranking.
Given two numeric values v and w the operation type of v and w is the operation
type of their run-time types.
Any numeric value type T supports the following methods.
• Comparison methods for equals (==), not-equals (!=), less-than (<), greater-
than (>), less-than-or-equals (<=), greater-than-or-equals (>=), which each ex-
ist in 7 overloaded alternatives. Each alternative takes a parameter of some
numeric value type. Its result type is type Boolean. The operation is evalu-
ated by converting the receiver and its argument to their operation type and
performing the given comparison operation of that type.
• Arithmetic methods addition (+), subtraction (-), multiplication (*), division
(/), and remainder (%), which each exist in 7 overloaded alternatives. Each
alternative takes a parameter of some numeric value type U . Its result type is
the operation type of T and U . The operation is evaluated by converting the
receiver and its argument to their operation type and performing the given
arithmetic operation of that type.
• Parameterless arithmethic methods identity (+) and negation (-), with result
type T . The first of these returns the receiver unchanged, whereas the second
returns its negation.
• Conversion methods toByte, toShort, toChar, toInt, toLong, toFloat,
12.2 Value Classes 143
toDouble which convert the receiver object to the target type, using the rules
of Java’s numeric type cast operation. The conversion might truncate the nu-
meric value (as when going from Long to Int or from Int to Byte) or it might
lose precision (as when going from Double to Float or when converting be-
tween Long and Float).
Numeric value types also implement operations equals, hashCode, and toString
from class Any.
The equals method tests whether the argument is a numeric value type. If this is
true, it will perform the == operation which is appropriate for that type. That is, the
equals method of a numeric value type can be thought of being defined as follows:
The hashCode method returns an integer hashcode that maps equal numeric val-
ues to equal results. It is guaranteed to be the identity for for type Int and for all
subrange types.
144 The Scala Standard Library
The toString method displays its receiver as an integer or floating point number.
Example 12.2.1 As an example, here is the signature of the numeric value type Int:
package scala
abstract sealed class Int extends AnyVal {
def == (that: Double): Boolean // double equality
def == (that: Float): Boolean // float equality
def == (that: Long): Boolean // long equality
def == (that: Int): Boolean // int equality
def == (that: Short): Boolean // int equality
def == (that: Byte): Boolean // int equality
def == (that: Char): Boolean // int equality
/* analogous for !=, <, >, <=, >= */
Class Boolean has only two values: true and false. It implements operations as
given in the following class definition.
package scala
abstract sealed class Boolean extends AnyVal {
def && (p: => Boolean): Boolean = // boolean and
if (this) p else false
def || (p: => Boolean): Boolean = // boolean or
if (this) true else p
def & (x: Boolean): Boolean = // boolean strict and
if (this) x else false
def | (x: Boolean): Boolean = // boolean strict or
if (this) true else x
def == (x: Boolean): Boolean = // boolean equality
if (this) x else x.unary_!
def != (x: Boolean): Boolean // boolean inequality
if (this) x.unary_! else x
def unary_!: Boolean // boolean negation
if (this) false else true
}
The class also implements operations equals, hashCode, and toString from class
Any.
The equals method returns true if the argument is the same boolean value as the
receiver, false otherwise. The hashCode method returns a fixed, implementation-
specific hash-code when invoked on true, and a different, fixed, implementation-
specific hash-code when invoked on false. The toString method returns the re-
ceiver converted to a string, i.e. either "true" or "false".
Class Unit has only one value: (). It implements only the three methods equals,
hashCode, and toString from class Any.
The equals method returns true if the argument is the unit value (), false oth-
erwise. The hashCode method returns a fixed, implementation-specific hash-code,
The toString method returns "()".
This section presents some standard Scala reference classes which are treated in a
special way in Scala compiler – either Scala provides syntactic sugar for them, or
the Scala compiler generates special code for their operations. Other classes in the
146 The Scala Standard Library
standard Scala library are documented in the Scala library documentation by HTML
pages.
Scala’s String class is usually derived from the standard String class of the underly-
ing host system (and may be identified with it). For Scala clients the class is taken
to support in each case a method
which concatenates its left operand with the textual representation of its right
operand.
Scala defines tuple classes Tuplen for n = 2, . . . , 22. These are defined as follows.
package scala
case class Tuplen [+T_1, ..., +T_n](_1: T_1, ..., _n : T_n ) {
def toString = "(" ++ _1 ++ "," ++ . . . ++ "," ++ _n ++ ")"
}
The implicitly imported Predef object (§12.5) defines the names Pair as an alias of
Tuple2 and Triple as an alias for Tuple3.
Scala defines function classes Functionn for n = 1, . . . , 22. These are defined as fol-
lows.
package scala
trait Functionn [-T_1, ..., -T_n , +R] {
def apply(x_1: T_1, ..., x_n : T_n ): R
def toString = "<function>"
}
The implicitly imported Predef object (§12.5) defines the name Function as an alias
of Function1.
If T is not a type parameter or abstract type, the type Array[T ] is represented as the
the array type |T |[] in the underlying host system, where |T | is the erasure of T .
If T is a type parameter or abstract type, a different representation might be used (it
is Object on the Java platform).
12.3.4.1 Operations
length returns the length of the array, apply means subscripting, and update means
element update.
Because of the syntactic sugar for apply and update operations, we have the follow-
ing correspondences between Scala and Java/C# code for operations on an array
xs:
Scala Java/C#
xs.length xs.length
xs(i) xs[i]
xs(i) = e xs[i] = e
Two implicit conversions exist in Predef that are frequently applied to ar-
rays: a conversion to scala.collection.mutable.ArrayOps and a conversion to
scala.collection.mutable.WrappedArray (a subtype of scala.collection.Seq).
Both types make many of the standard operations found in the Scala collections
API available. The conversion to ArrayOps is temporary, as all operations defined
on ArrayOps return a value of type Array, while the conversion to WrappedArray is
permanent as all operations return a value of type WrappedArray. The conversion to
ArrayOps takes priority over the conversion to WrappedArray.
Because of the tension between parametrized types in Scala and the ad-hoc imple-
148 The Scala Standard Library
mentation of arrays in the host-languages, some subtle points need to be taken into
account when dealing with arrays. These are explained in the following.
12.3.4.2 Variance
Unlike arrays in Java or C#, arrays in Scala are not co-variant; That is, S <: T does
not imply Array[S ] <: Array[T ] in Scala. However, it is possible to cast an array
of S to an array of T if such a cast is permitted in the host environment.
For instance Array[String] does not conform to Array[Object], even though
String conforms to Object. However, it is possible to cast an expression of type
Array[String] to Array[Object], and this cast will succeed without raising a
ClassCastException. Example:
import reflect.ClassTag
def mkArray[T : ClassTag](elems: Seq[T]): Array[T] = {
val result = new Array[T](elems.length)
var i = 0
for (elem <- elems) {
result(i) = elem
i += 1
}
result
}
If type T is a type for which the host platform offers a specialized array representa-
tion, this representation is used.
mkArray(List(1,2,3))
Array’s companion object provides various factory methods for the instantiation
of single- and multi-dimensional arrays, an extractor method unapplySeq (§8.1.8)
which enables pattern matching over arrays and additional utility methods:
package scala
object Array {
/** copies array elements from ‘src’ to ‘dest’. */
def copy(src: AnyRef, srcPos: Int,
dest: AnyRef, destPos: Int, length: Int): Unit = . . .
/** Returns an array that contains the results of some element computation a number
* of times. */
def fill[T: ClassTag](n: Int)(elem: => T): Array[T] = . . .
/** Returns a two-dimensional array that contains the results of some element
* computation a number of times. */
def fill[T: ClassTag](n1: Int, n2: Int)(elem: => T): Array[Array[T]] = . . .
...
/** Returns an array containing values of a given function over a range of integer
* values starting from 0. */
def tabulate[T: ClassTag](n: Int)(f: Int => T): Array[T] = . . .
/** Returns a two-dimensional array containing values of a given function
* over ranges of integer values starting from ‘0‘. */
def tabulate[T: ClassTag](n1: Int, n2: Int)(f: (Int, Int) => T): Array[Array[T]] = . . .
...
package scala.xml
trait Node {
/** XPath style projection function. Returns all children of this node
* that are labeled with ’that’. The document order is preserved.
12.4 Class Node 151
*/
def \(that: Symbol): NodeSeq = {
new NodeSeq({
that.name match {
case "_" => child.toList
case _ =>
var res:List[Node] = Nil
for (x <- child.elements if x.label == that.name) {
res = x::res
}
res.reverse
}
})
}
/** XPath style projection function. Returns all nodes labeled with the
* name ’that’ from the ’descendant_or_self’ axis. Document order is preserved.
*/
def \\(that: Symbol): NodeSeq = {
new NodeSeq(
that.name match {
case "_" => this.descendant_or_self
case _ => this.descendant_or_self.asInstanceOf[List[Node]].
filter(x => x.label == that.name)
})
}
}
152 The Scala Standard Library
The Predef object defines standard functions and type aliases for Scala programs.
It is always implicitly imported, so that all its defined members are available with-
out qualification. Its definition for the JVM environment conforms to the following
signature:
package scala
object Predef {
// classOf ---------------------------------------------------------
// Miscellaneous -----------------------------------------------------
// tupling ---------------------------------------------------------
...
}
The Predef object also contains a number of implicit definitions, which are avail-
able by default (because Predef is implicitly imported). Implicit definitions come in
12.5 The Predef Object 155
two priorities. High-priority implicits are defined in the Predef class itself whereas
low priority implicits are defined in a class inherited by Predef. The rules of static
overloading resolution (§6.26.3) stipulate that, all other things being equal, implicit
resolution prefers high-priority implicits over low-priority ones.
The available low-priority implicits include definitions falling into the following cat-
egories.
1. For every primitive type, a wrapper that takes values of that type to instances
of a runtime.Rich* class. For instance, values of type Int can be implicitly
converted to instances of class runtime.RichInt.
2. For every array type with elements of primitive type, a wrapper that takes the
arrays of that type to instances of a runtime.WrappedArray class. For instance,
values of type Array[Float] can be implicitly converted to instances of class
runtime.WrappedArray[Float]. There are also generic array wrappers that
take elements of type Array[T] for arbitrary T to WrappedArrays.
3. An implicit conversion from String to WrappedString.
The available high-priority implicits include definitions falling into the following
categories.
• An implicit wrapper that adds ensuring methods with the following over-
loaded variants to type Any.
• An implicit wrapper that adds a -> method with the following implementation
to type Any.
• For every array type with elements of primitive type, a wrapper that takes the
arrays of that type to instances of a runtime.ArrayOps class. For instance,
values of type Array[Float] can be implicitly converted to instances of class
runtime.ArrayOps[Float]. There are also generic array wrappers that take
elements of type Array[T] for arbitrary T to ArrayOpss.
• An implicit wrapper that adds + and formatted method with the following
implementations to type Any.
• Boxing and unboxing conversions between primitive types and their boxed
versions:
• An implicit definition that generates instances of type T <:< T, for any type T.
Here, <:< is a class defined as follows.
Implicit parameters of <:< types are typically used to implement type con-
straints.
Bibliography
[Oa04] Martin Odersky and al. An Overview of the Scala Programming Lan-
guage. Technical Report IC/2004/64, EPFL Lausanne, Switzerland, 2004.
[OCRZ03] Martin Odersky, Vincent Cremet, Christine Röckl, and Matthias Zenger.
A Nominal Theory of Objects with Dependent Types. In Proc. ECOOP’03,
Springer LNCS, July 2003.
[Ode06] Martin Odersky. The Scala Experiment – Can We Provide Better Lan-
guage Support for Component Systems? In Proc. ACM Symposium on
Principles of Programming Languages, 2006.
The lexical syntax of Scala is given by the following grammar in EBNF form.
upper ::= ‘A’ | · · · | ‘Z’ | ‘$’ | ‘_’ and Unicode category Lu
lower ::= ‘a’ | · · · | ‘z’ and Unicode category Ll
letter ::= upper | lower and Unicode categories Lo, Lt, Nl
digit ::= ‘0’ | · · · | ‘9’
opchar ::= “all other characters in \u0020-007F and Unicode
categories Sm, So except parentheses ([]) and periods”
floatingPointLiteral
::= digit {digit} ‘.’ digit {digit} [exponentPart] [floatType]
| ‘.’ digit {digit} [exponentPart] [floatType]
| digit {digit} exponentPart [floatType]
| digit {digit} [exponentPart] floatType
exponentPart ::= (‘E’ | ‘e’) [‘+’ | ‘-’] digit {digit}
floatType ::= ‘F’ | ‘f’ | ‘D’ | ‘d’
Change Log
Trailing commas
The precedence of assignment operators has been brought in line with Java’s
(§6.12). From now on, += has the same precedence as =.
The Unicode glyph \u2190 ‘←’ is now treated as a reserved identifier, equivalent to
the ASCII symbol ‘<-’.
A wildcard in a type now binds to the closest enclosing type application. For exam-
ple List[List[_]] is now equivalent to the existential type
The new convention corresponds exactly to the way wildcards in Java are inter-
preted.
The contractiveness requirement for implicit method definitions has been dropped.
Instead it is checked for each implicit expansion individually that the expansion
does not result in a cycle or a tree of infinitely growing types (§7.2).
169
Java Generics
This translation works if -target:jvm-1.5 is specified, which is the new default. For
any other target, Java generics are not recognized. To ensure upgradability of Scala
codebases, extraneous type parameters for Java classes under -target:jvm-1.4
are simply ignored. For instance, when compiling with -target:jvm-1.4, a Scala
type such as ArrayList[String] is simply treated as the unparameterized type
ArrayList.
The Scala compiler generates now for every case class a companion extractor object
(§5.3.2). For instance, given the case class:
object X {
def unapply(x: X): Some[String] = Some(x.elem)
def apply(s: String): X = new X(s)
}
If the object exists already, only the apply and unapply methods are added to it.
Three restrictions on case classes have been removed.
Self-types
Self types can now be introduced without defining an alias name for this (§5.1).
Example:
class C {
type T <: Trait
trait Trait { this: T => ... }
}
Existential types
It is now possible to define existential types (§3.2.10). An existential type has the
form T forSome {Q} where Q is a sequence of value and/or type declarations. Given
the class definitions
class Ref[T]
abstract class Outer { type T }
Lazy values
It is now possible to define lazy value declarations using the new modifier lazy
(§4.1). A lazy value definition evaluates its right hand side e the first time the value
is accessed. Example:
import compat.Platform._
val t0 = currentTime
lazy val t1 = currentTime
171
val t2 = currentTime
Structural types
It is now possible to declare structural types using type refinements (§3.2.7). For
example:
test(new File("test.txt"))
test(new java.io.File("test.txt"))
There’s also a shorthand form for creating values of structural types. For instance,
is a shorthand for
Type parameters (§4.4) and abstract type members (§4.3) can now also abstract over
type constructors (§3.3.3).
This allows a more precise Iterable interface:
trait Iterable[+T] {
type MyType[+T] <: Iterable[T] // MyType is a type constructor
1
Implemented by Adriaan Moors
172 Change Log
This definition of Iterable makes explicit that mapping a function over a certain
structure (e.g., a List) will yield the same structure (containing different elements).
It is now possible to initialize some fields of an object before any parent constructors
are called (§5.1.6). This is particularly useful for traits, which do not have normal
constructor parameters. Example:
trait Greeting {
val name: String
val msg = "How are you, "+name
}
class C extends {
val name = "Bob"
} with Greeting {
println(msg)
}
In the code above, the field name is initialized before the constructor of Greeting
is called. Therefore, field msg in class Greeting is properly initialized to
"How are you, Bob".
For-comprehensions, revised
The syntax of for-comprehensions has changed (§6.19). In the new syntax, gener-
ators do not start with a val anymore, but filters start with an if (and are called
guards). A semicolon in front of a guard is optional. For example:
is now written
The old syntax is still available but will be deprecated in the future.
_ + 1 x => x + 1
_ * _ (x1, x2) => x1 * x2
(_: int) * 2 (x: int) => (x: int) * 2
if (_) x else y z => if (z) x else y
_.map(f) x => x.map(f)
_.map(_ + 1) x => x.map(y => y + 1)
It is now possible to use case clauses to define a function value directly for functions
of arities greater than one (§8.5). Previously, only unary functions could be defined
that way. Example:
The private and protected modifiers now accept a [this] qualifier (§5.2). A defini-
tion M which is labelled private[this] is private, and in addition can be accessed
only from within the current object. That is, the only legal prefixes for M are this
or C .this. Analogously, a definition M which is labelled protected[this] is pro-
tected, and in addition can be accessed only from within the current object.
Tuples, revised
The syntax for tuples has been changed from {. . .} to (. . .) (§6.9). For any sequence of
types T1 , . . . , Tn ,
(T1 , . . . , Tn ) is a shorthand for Tuplen [T1 , . . . , Tn ].
The primary constructor of a class can now be marked private or protected (§5.3).
If such an access modifier is given, it comes between the name of the class and its
value parameters. Example:
Annotations
The support for attributes has been extended and its syntax changed (§11). At-
tributes are now called annotations. The syntax has been changed to follow Java’s
conventions, e.g. @attribute instead of [attribute]. The old syntax is still avail-
able but will be deprecated in the future.
Annotations are now serialized so that they can be read by compile-time or run-
time tools. Class scala.Annotation has two sub-traits which are used to indicate
how annotations are retained. Instances of an annotation class inheriting from trait
scala.ClassfileAnnotation will be stored in the generated class files. Instances
of an annotation class inheriting from trait scala.StaticAnnotation will be visible
to the Scala type-checker in every compilation unit where the annotated symbol is
accessed.
Decidable subtyping
It is now explicitly ruled out that case classes can be abstract (§5.2). The specifica-
tion was silent on this point before, but did not explain how abstract case classes
were treated. The Scala compiler allowed the idiom.
It is now possible to give an explicit alias name and/or type for the self reference
this (§5.1). For instance, in
the name self is introduced as an alias for this within C and the self type (§5.3) of C
is assumed to be D. This construct is introduced now in order to replace eventually
175
both the qualified this construct C.this and the requires clause in Scala.
Assignment Operators
var x: int = 0
x += 1
Extractors
object Twice {
def apply(x:Int): int = x*2
def unapply(z:Int): Option[int] = if (z%2==0) Some(z/2) else None
}
val x = Twice(21)
x match { case Twice(n) => Console.println(n) } // prints 21
Tuples
A new lightweight syntax for tuples has been introduced (§6.9). For any sequence of
types T1 , . . . , Tn ,
{T1 , . . . , Tn } is a shorthand for Tuplen [T1 , . . . , Tn ].
176 Change Log
It is now possible to use methods which have more than one parameter as infix
operators (§6.12). In this case, all method arguments are written as a normal pa-
rameter list in parentheses. Example:
class C {
def +(x: int, y: String) = ...
}
val c = new C
c + (1, "abc")
Deprecated attribute
Procedures
A simplified syntax for functions returning unit has been introduced (§4.6.3). Scala
now allows the following shorthands:
Type Patterns
The syntax of types in patterns has been refined (§8.2). Scala now distinguishes be-
tween type variables (starting with a lower case letter) and types as type arguments
in patterns. Type variables are bound in the pattern. Other type arguments are,
as in previous versions, erased. The Scala compiler will now issue an “unchecked”
warning at places where type erasure might compromise type-safety.
Standard Types
The recommended names for the two bottom classes in Scala’s type hierarchy have
changed as follows:
177
Private members of a class can now be referenced from the companion module of
the class and vice versa (§5.2)
Implicit Lookup
The lookup method for implicit definitions has been generalized (§7.2). When
searching for an implicit definition matching a type T , now are considered
(The second clause is more general than before). Here, a class is associated with a
type T if it is referenced by some part of T , or if it is a base class of some part of T .
For instance, to find implicit members corresponding to the type
HashSet[List[Int], String]
one would now look in the companion modules (aka static parts) of HashSet, List,
Int, and String. Before, it was just the static part of HashSet.
A typed pattern match with a singleton type p.type now tests whether the selector
value is reference-equal to p (§8.1). Example:
val p = List(1, 2, 3)
178 Change Log
val q = List(1, 2)
val r = q
r match {
case _: p.type => Console.println("p")
case _: q.type => Console.println("q")
}
This will match the second case and hence will print "q". Before, the singleton types
were erased to List, and therefore the first case would have matched, which is non-
sensical.
"""this is a
multi-line
string literal"""
No escape substitutions except for unicode escapes are performed in such string
literals.
Closure Syntax
The syntax of closures has been slightly restricted (§6.23). The form
x: T => E
is valid only when enclosed in braces, i.e. { x: T => E }. The following is illegal,
because it might be read as the value x typed with the type T => E:
val f = x: T => E
val f = { x: T => E }
val f = (x: T) => E
179
Class Literals
There is a new syntax for class literals (§6.2): For any class type C , classOf[C ] des-
ignates the run-time representation of C .
Scala in its second version is different in some details from the first version of the
language. There have been several additions and some old idioms are no longer
supported. This appendix summarizes the main changes.
New Keywords
The following three words are now reserved; they cannot be used as identifiers (§1.1)
Syntax Restrictions
There are some other situations where old constructs no longer work:
Pattern matching expressions. The match keyword now appears only as infix op-
erator between a selector expression and a number of cases, as in:
expr match {
case Some(x) => ...
case None => ...
}
Variants such as expr.match {...} or just match {...} are no longer sup-
ported.
Regular Expression Patterns. The only form of regular expression pattern that is
currently supported is a sequence pattern, which might end in a sequence wildcard
_*. Example:
case List(1, 2, _*) => ... // will match all lists starting with \code{1,2}.
Selftype Annotations
becomes
That is, selftypes are now indicated by the new requires keyword. The old syntax is
still available but is considered deprecated.
For-comprehensions
for {
val x <- List.range(1, 100)
val y <- List.range(1, x)
val z = x + y
isPrime(z)
} yield Pair(x, y)
Conversions
The rules for implicit conversions of methods to functions (§6.26) have been tight-
ened. Previously, a parameterized method used as a value was always implicitly
converted to a function. This could lead to unexpected results when method argu-
ments where forgotten. Consider for instance the statement below:
show(x.toString)
Most likely, the programmer forgot to supply an empty argument list () to toString.
The previous Scala version would treat this code as a partially applied method, and
expand it to:
The partial application of sum in the last line of the code above will not be converted
to a function type. Instead, the compiler will produce an error message which states
that arguments for method sum are missing. The problem can be fixed by providing
an expected type for the partial application, for instance by annotating the defini-
tion of sumInts with its type:
On the other hand, Scala version 2.0 now automatically applies methods with empty
parameter lists to () argument lists when necessary. For instance, the show expres-
sion above will now be expanded to
show(x.toString()) .
Scala version 2.0 also relaxes the rules of overriding with respect to empty parameter
lists. The revised definition of matching members (§5.1.3) makes it now possible to
182 Change Log
override a method with an explicit, but empty parameter list () with a parameterless
method, and vice versa. For instance, the following class definition is now legal:
class C {
override def toString: String = ...
}
Previously this definition would have been rejected, because the toString method
as inherited from java.lang.Object takes an empty parameter list.
Class Parameters
Private Qualifiers
Previously, Scala had three levels of visibility: private, protected and public. There
was no way to restrict accesses to members of the current package, as in Java. Scala
2 now defines access qualifiers that let one express this level of visibility, among
others. In the definition
access to f is restricted to all code within the class or package C (which must contain
the definition of f) (§5.2)
The model which details mixin composition of classes has changed significantly.
The main differences are:
1. We now distinguish between traits that are used as mixin classes and normal
classes. The syntax of traits has been generalized from version 1.0, in that
traits are now allowed to have mutable fields. However, as in version 1.0, traits
still may not have constructor parameters.
2. Member resolution and super accesses are now both defined in terms of a
class linearization.
Implicit Parameters
Views in Scala 1.0 have been replaced by the more general concept of implicit pa-
rameters (§7)
The new version of Scala implements more flexible typing rules when it comes to
pattern matching over heterogeneous class hierarchies (§8.4). A heterogeneous class
hierarchy is one where subclasses inherit a common superclass with different pa-
rameter types. With the new rules in Scala version 2.0 one can perform pattern
matches over such hierarchies with more precise typings that keep track of the infor-
mation gained by comparing the types of a selector and a matching pattern (§Exam-
ple 8.4.1). This gives Scala capabilities analogous to guarded algebraic data types.