Reserved Words - Learning Scala (2015)

Learning Scala (2015)

Appendix A. Reserved Words

Table A-1 displays the reserved words in Scala. Reserved words are part of the Scala language definition, and cannot be used as identifiers. To keep the definitions concise, I have used “class” where “class, object, and trait” may be more accurate.

Table A-1. Scala’s reserved words

Name

Description

_

The wildcard operator, representing an expected value.

:

Delimits a value, variable, or function from its type.

@

Defines an annotation for a class or its member. Annotations are a JVM feature but are seldomly used in Scala, with @annotation.tailrec being a popular exception.

#

A type projection, which delimits a type from its subtype.

<-

Delimits a generator from its identifier in a for-loop.

A single-character (\u2190) alternative to <-.

<:

The upper-bound operator, restricting types to those that are equal to or extend the given type.

<%

The view-bound operator, allowing any type that may be treated as the given type.

=

The assignment operator.

=>

Used in match expressions and partial functions to indicate a conditional expression, in function types to indicate a return type, and in function literals to define the function body.

A single-character (\u21D2) alternative to =>.

>:

The lower-bound operator, restricting types to those that are equal to or are extended by the given type.

abstract

Marks a class or trait as being abstract and uninstantiable.

case

Defines a matching pattern in match expressions and partial functions.

catch

Catches an exception. An alternate syntax that predates the util.Try monadic collection.

class

Defines a new class.

def

Defines a new method.

do

Part of the do..while loop definition.

else

The second part of an if..else conditional expression.

extends

Defines a base type for a class.

false

One of the two Boolean values.

final

Marks a class or trait as being nonextendable.

finally

Executes an expression following a try block. An alternate syntax that predates the util.Try monadic collection.

for

Begins a for-loop.

forSome

Defines an existential type. Existential types are a flexible method for specifying type requirements, but are discouraged in general Scala development. See SIP-18 (Scala Improvement Process #18) for details on why existential types are considered an “opt-in” feature in Scala.

if

The first part of an if..else conditional expression, or the main part of an if conditional statement.

implicit

Defines an implicit conversion or parameter.

import

Imports a package, class, or members of a class to the current namespace.

lazy

Defines a value as being lazy, only defined the first time it is accessed.

match

Begins a match expression.

new

Creates a new instance of a class.

null

A value that indicates the lack of an instance. Has the type Null.

object

Defines a new object.

override

Marks a value or method as replacing the member of the same name in a base type.

package

Defines the current package, an incremental package name, or a package object.

private

Marks a class member as being inaccessible outside the class definition.

protected

Marks a class member as being inaccessible outside the class definition or its subclasses.

return

Explicitly states the return value for a method. By default, the last expression in a method is used as the return value.

sealed

Marks a class as only allowing subclasses within the current file.

super

Marks a class member reference as one in the base type, versus one overridden in the current class.

this

Marks a class member reference as one in the current class, versus a parameter with the same name.

throw

Raises an error condition that breaks the current flow of operation and only resumes if the error is caught elsewhere.

trait

Defines a new trait.

true

One of the two Boolean values.

try

Marks a range of code for catching an exception. An alternate syntax that predates the util.Try monadic collection.

type

Defines a new type alias.

val

Defines a new, immutable value.

var

Defines a new, mutable variable.

while

Part of the do..while loop definition.

with

Defines a base trait for a class.

yield

Yields the return value from a for-loop.

WHERE ARE MY FAVORITE :: AND ++ OPERATORS?

The :: and ++ operators are valid method identifiers, not reserved words. The Scala collections library defines methods with these identifiers, which means you can also use them for your own methods.