Scala tips and code snippets.
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:
_ |
: |
= |
=> , ⇒ |
<- , ← |
<% |
<: |
>: |
# |
@ |
* |
( ) |
[ ] |
{ } |
// , /* */ |
" , """ |
' |
` |
, |
; |
_
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
}
:
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.
=
val x = 5
varName = 5
var x: Type = _
0
, false
, Unit
or null
.x(1, 2) = 3
x.update(1, 2, 3)
val x(param) = y
val param = x.unapply(y)
def methodName = ...
def methodName(param: Type = 4) = ...
class ClassName(param: Type = 4) = ...
.type name = Type
=>
, ⇒
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 }
<-
, ←
for (x <- itemList) { ... }
<%
def methodName[TypeAlias <% Converter[TypeAlias]](param: TypeAlias) ...
Converter
generic class.
This notation called
view bounds is deprecated!<:
type TypeAlias <: SuperType
SuperType
.>:
type TypeAlias >: SubType
SubType
.#
val x OuterType#InnerType
InnerType
is defined inside an other type OuterType
.
This notation is called type projection.@
@annotation
x match { case a@(1 | 2) => a + 1 }
a
with x
value, when x
matches the pattern (1 | 2)
.
This notation is called pattern binder.*
def myMethod(args: Type*)
val x = myMethod(argsList:_*)
x match { case CaseClassName(a, _*) => a + 1 }
case class
during pattern matching.( )
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) ...
while (...)
& do ... while (...)
expressions.for (x <- items) ...
(x + 1) + 2
[ ]
def methodName[TypeAlias](param: TypeAlias)...
private[EnclosingClass] val classMember = 2
protected
values with special restrictions can also be defined this way.private[this] val classMember = 3
protected
values with special restrictions can also be defined this way.{ }
{ x + 1 }
import packagename.{Name1, Name2 => NewName2, ...}
class ClassName { val x = 1 }
x match { case ... }
match
block.for { x <- items } ...
try ... catch { case ... }
catch
block.package name { ... }
val x = new TraitName { ... }
type T = A forSome { type A }
//
, /* */
// inline comment
is an inline comment.
/*
multi-lines
comment
*/
is a multi-lines comment.
"
, """
val x = "text"
defines a string value. The following code defines a multilines string value:
val x = """
long
text
"""
'
val c = 'X'
val x = 'symbolValue
Map
keys for example.`
val `reservedKeywords` = 2
val `case` = 1
.,
def methodName(x: Type1, y: Type2)
methodName(x, y)
GenericType[Type1, Type2]
val x, y, z = 3
import packagename.{Name1, Name2, ...}
;
val x = 6; x + 1
This project is maintained by
Y. Somda (yoeo)
Logo by
Icon Island
— theme by
orderedlist
— remixed by
yoeo