Scala Explained!

Scala tips and code snippets.

Scala symbols explained

Here we will explain all Scala language reserved symbols.

Reserved symbols are special character, punctuation that have a special meaning in Scala. They cannot be used as an identifier (variable name or class name). For each reserved symbol there is a list of use cases and for each use case there is a concise explanation. The uses cases and explanations are mostly taken from the Scala language specification

Note that operators +, -, etc.. are not always reserved symbols. They are identifiers that can be redefined.

There are currently 20 reserved symbols in Scala language:

       
_ : = =>,
<-, <% <: >:
# @ * ( )
[ ] { } //, /* */ ", """
' ` , ;

underscore _

x match { case CaseClassName(_, _) => 33 }

here _ is a placeholder for unused parameters. An other example: x match { case _ => 0 }.

import packagename._

imports every accessible identifier that is in the package.

import packagename.{name => _, _}

imports everything but name.

val functionName:Type1 => Type2 = (_.toType2)

curried form of a function definition. Same as val functionName:Type1 => Type2 = (x) => x.toType2. The curried form can also be used for function calls, example: List(1, 2, 3).map(_ + 2) // --> List(3, 4, 5).

val x = tupleName._1

accesses the first element of a tuple.

val functionName = method(_, _, _)

creates a new function from an existing one using currying.

var x: Type = _

creates a variable that contains an empty value.

methodName(argList:_*)

expands the parameters when calling a method that has a variable number of parameters.

def unary_- = 0 - this.x

defines the unary operator -x for a class instances.

def methodName(param: GenericType[_])...

defines a method that takes a parameter that has a generic existential type. _ here is a placeholder for any type.

def member_= ...

is part of the definition of a private class member getter and setter. Full example:

  class ClassName {
    private var _member: Long = _
    // getter
    def member = _member
    // setter
    def member_= (x: Long): Unit =
      _member = x
  }

colon :

val name: Type = ...

defines the type of an identifier.

(x: @unchecked) match { case _ => ... }

annotates an expression.

methodName(argList:_*)

expands the parameters when calling a method that has a variable number of parameters.

equals sign =

val x = 5
varName = 5
var x: Type = _
x(1, 2) = 3
val x(param) = y
def methodName = ...
def methodName(param: Type = 4) = ...
type name = Type

rightward double arrow =>,

val x:(Type1, Type2) => ReturnType = ...
val x = (x: Type) => x + 3
def methodName(param => Type) ...
import mypackage.{Type => NewType}
{ this: InjectedClassName => ... }
x match { case _: => 0 }

leftward arrow <-,

for (x <- itemList) { ... }

less than percent <%

def methodName[TypeAlias <% Converter[TypeAlias]](param: TypeAlias) ...

less than colon <:

type TypeAlias <: SuperType

more than colon >:

type TypeAlias >: SubType

hashtag #

val x OuterType#InnerType

at @

@annotation
x match { case a@(1 | 2) => a + 1 }

star *

def myMethod(args: Type*)
val x = myMethod(argsList:_*)
x match { case CaseClassName(a, _*) => a + 1 }

parenthesis ( )

def methodName(param1: Type1, param2: Type2)...
(x: Type1, y:Type2) => x + y
val x = methodName(param1, param2)
val x = (1, "text")
var x:(Type1, Type2) = _
if (x == 1) ...
for (x <- items) ...
(x + 1) + 2

brackets [ ]

def methodName[TypeAlias](param: TypeAlias)...
private[EnclosingClass] val classMember = 2
private[this] val classMember = 3

curly brackets { }

{ x + 1 }
import packagename.{Name1, Name2 => NewName2, ...}
class ClassName { val x = 1 }
x match { case ... }
for { x <- items } ...
try ... catch { case ... }
package name { ... }
val x = new TraitName { ... }
type T = A forSome { type A }

comment signs //, /* */

// inline comment

double quotes ", """

val x = "text"

single quotes '

val c = 'X'
val x = 'symbolValue

back quotes `

val `reservedKeywords` = 2

comma ,

def methodName(x: Type1, y: Type2)
methodName(x, y)
GenericType[Type1, Type2]
val x, y, z = 3
import packagename.{Name1, Name2, ...}

semicolon ;

val x = 6; x + 1
You can leave a comment or request changes here.

This project is maintained by Y. Somda (yoeo)
Logo by Icon Island — theme by orderedlist — remixed by yoeo