Scala tips and code snippets.
We will take a tour of all Scala language reserved keywords.
Reserved keywords are words that have a special meaning in Scala. They cannot be used as an identifier (variable name or class name). For each reserved keyword 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
There are currently 40 reserved keywords in Scala language:
Let’s explain them all!
abstract class ClassName { ... }
abstract override def traitMethod = ...
x match {
case 1 => ...
case 2 => ...
...
}
x
value matches one of the cases, the associated statement
is executed.
This feature is called pattern matching.x match {
case a if (a > 2) => ...
}
try {
...
} catch {
case error: ErrorClassName => ...
}
case class ClassName(param: Type, ...)
a == b
,case object ObjectName { ... }
def x: Type1 => Type2 = {
case x => ...
}
try { ... } catch { ... }
class ClassName { ... }
Defines a class. A class is a blueprint for values called instances. A class can be defined with the following modifiers:
abstract class ClassName { ... }
final class ClassName { ... }
sealed class ClassName { ... }
case class ClassName(...)
def myFunction(...) = { ... }
do {
...
} while (...)
do
block as long as
the while condition is true.
That is a do-while loopif (a > 2) ... else ...
class ClassName extends BaseClassName
val x: Boolean = false
false
.final class ClassName { ... }
final def classMember = ...
final val x = 10
try {
...
} catch {
...
} finally {
...
}
for (x <- items) ...
items
.for (x <- items) yield ...
for (x <- items if (x > 2)) ...
type T = A forSome { type A }
if (a > 2) ... else ...
for (x <- items if (x > 2)) ...
if
condition is false the current element is skipped.x match {
case a if (a > 2) => ...
}
x
value
matches the pattern and the if
condition is true.def methodName(implicit param: String) = ...
implicit val x = 2
import packagename.ClassName
import packagename._
import packagename.{Name1, Name2, ...}
import packagename.{Name => NewName}
lazy val x = ...
def methodName: Unit = macro methodImplementation
x match {
case 1 => ...
case 2 => ...
}
x
value.val x = new ClassName
val x: String = null
x
.
Null value exists for compatibility reasons with Java.
Because Arnold hates NullPointerException
it is safer to not explicitly use null
and handle
null
values through optional values.object ObjectName
case object ObjectName
package object ObjectName
import ObjectName.memberName
.override val classMember = 2
abstract override def traitMethod = ...
package packagename
package name { ... }
package object ObjectName
import ObjectName.memberName
private val classMember = 1
private[EnclosingClass] val classMember = 2
private[this] val classMember = 3
protected val classMember = 1
protected[EnclosingClass] val classMember = 2
protected[this] val classMember = 2
return x
sealed class ClassName
sealed
and final modifiers
cannot be used on the same class.super.superClassMember
this.classMember
this
represents the current instance
of the class.class ClassName {
this: InjectedClassName =>
...
}
def this(param: Int) = this(param, 2L, ...)
private[this] val classMember = 2
protected[this] val classMember = 2
throw new ErrorClass(message)
trait TraitName { ... }
try {
...
} catch {
...
}
try
block. The error is caught
and processed through pattern matching.val x: Boolean = true
true
.type T
val x = 1
var x = 2
do {
...
} while (...)
while
condition is false.while (...) {
...
}
while
condition is false.class ClassName extends Base1 with Trait1 with Trait2
val x = new ClassName with Trait1 with Trait2
for (x <- items) yield ...
This project is maintained by
Y. Somda (yoeo)
Logo by
Icon Island
— theme by
orderedlist
— remixed by
yoeo