Table lists the reserved keywords in Scala. Many are found in Java and they usually have the same meanings in both languages.
Word | Description |
---|---|
abstract | Makes a declaration abstract. |
case | Start a case clause in a match expression. Define a “case class.” |
catch | Start a clause for catching thrown exceptions. |
class | Start a class declaration |
def | Start a method declaration. |
do | Start a do…while loop. |
else | Start an else clause for an if clause. |
extends | Indicates that the class or trait that follows is the parent type of the class or trait being declared. |
false | Boolean false . |
final | Applied to a class or trait to prohibit deriving child types from it. Applied to a member to prohibit overriding it in a derived class or trait . |
finally | Start a clause that is executed after the corresponding try clause, whether or not an exception is thrown by the try clause. |
for | Start a for comprehension (loop). |
forSome | Used in existential type declarations to constrain the allowed concrete types that can be used. |
if | Start an if clause. |
implicit | Marks a method or value as eligible to be used as an implicit type converter or value. Marks a method parameter as optional, as long as a type-compatible substitute object is in the scope where the method is called. |
import | Import one or more types or members of types into the current scope. |
lazy | Defer evaluation of a val . |
match | Start a pattern-matching clause. |
new | Create a new instance of a class. |
null | Value of a reference variable that has not been assigned a value. |
object | Start a singleton declaration: a class with only one instance. |
override | Override a concrete member of a type, as long as the original is not marked final . |
package | Start a package scope declaration. |
private | Restrict visibility of a declaration. |
protected | Restrict visibility of a declaration. |
requires | Deprecated. Was used for self-typing. |
return | Return from a function. |
sealed | Applied to a parent type. Requires all derived types to be declared in the same source file. |
super | Analogous to this , but binds to the parent type. |
this | How an object refers to itself. The method name for auxiliary constructors. |
throw | Throw an exception. |
trait | A mixin module that adds additional state and behavior to an instance of a class. Can also be used to just declare methods, but not define them, like a Java interface. |
try | Start a block that may throw an exception. |
true | Boolean true . |
type | Start a type declaration. |
val | Start a read-only “variable” declaration. |
var | Start a read-write variable declaration. |
while | Start a while loop. |
with | Include the trait that follows in the class being declared or the object being instantiated. |
yield | Return an element in a for comprehension that becomes part of a sequence. |
_ | A placeholder, used in imports, function literals, etc. |
: | Separator between identifiers and type annotations. |
= | Assignment. |
=> | Used in function literals to separate the argument list from the function body. |
<- | Used in for comprehensions in generator expressions. |
<: | Used in parameterized and abstract type declarations to constrain the allowed types. |
<% | Used in parameterized and abstract type “view bounds” declarations. |
>: | Used in parameterized and abstract type declarations to constrain the allowed types. |
# | Used in type projections. |
@ | Marks an annotation. |
⇒ | (Unicode \u21D2) Same as => . |
→ | (Unicode \u2192) Same as -> . |
← | (Unicode \u2190) Same as <- . |
Note:
1. break
and continue
are not listed. These control keywords don’t exist in Scala.
2. Some Java methods use names that are reserved words by Scala, for example, java.util.Scanner.match
. To avoid a compilation error, surround the name with single back quotes (“back ticks”), e.g., java.util.Scanner.`match`.
Ref