Scala Explained!

Scala tips and code snippets.

Scala keywords explained

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:

         
abstract case catch class def
do else extends false final
finally for forSome if implicit
import lazy macro match new
null object override package private
protected return sealed super this
throw trait try true type
val var while with yield

Let’s explain them all!

abstract

abstract class ClassName { ... }
abstract override def traitMethod = ...

case

x match {
  case 1 => ...
  case 2 => ...
  ...
}
x match {
  case a if (a > 2) => ...
}
try {
  ...
} catch {
  case error: ErrorClassName => ...
}
case class ClassName(param: Type, ...)
case object ObjectName { ... }
def x: Type1 => Type2 = {
  case x => ...
}

catch

try { ... } catch { ... }

class

class ClassName { ... }
case class ClassName(...)

def

def myFunction(...) = { ... }

do

do {
  ...
} while (...)

else

if (a > 2) ... else ...

extends

class ClassName extends BaseClassName

false

val x: Boolean = false

final

final class ClassName { ... }
final def classMember = ...
final val x = 10

finally

try {
  ...
} catch {
  ...
} finally {
  ...
}

for

for (x <- items) ...
for (x <- items) yield ...
for (x <- items if (x > 2)) ...

forSome

type T = A forSome { type A }

if

if (a > 2) ... else ...
for (x <- items if (x > 2)) ...
x match {
  case a if (a > 2) => ...
}

implicit

def methodName(implicit param: String) = ...
implicit val x = 2

import

import packagename.ClassName
import packagename._
import packagename.{Name1, Name2, ...}
import packagename.{Name => NewName}

lazy

lazy val x = ...

macro

def methodName: Unit = macro methodImplementation

match

x match {
  case 1 => ...
  case 2 => ...
}

new

val x = new ClassName

null

val x: String = null

object

object ObjectName
case object ObjectName
package object ObjectName

override

override val classMember = 2
abstract override def traitMethod = ...

package

package packagename
package name { ... }
package object ObjectName

private

private val classMember = 1
private[EnclosingClass] val classMember = 2
private[this] val classMember = 3

protected

protected val classMember = 1
protected[EnclosingClass] val classMember = 2
protected[this] val classMember = 2

return

return x

sealed

sealed class ClassName

super

super.superClassMember

this

this.classMember
class ClassName {
  this: InjectedClassName =>
  ...
}
def this(param: Int) = this(param, 2L, ...)
private[this] val classMember = 2
protected[this] val classMember = 2

throw

throw new ErrorClass(message)

trait

trait TraitName { ... }

try

try {
  ...
} catch {
  ...
}

true

val x: Boolean = true

type

type T

val

val x = 1

var

var x = 2

while

do {
  ...
} while (...)
while (...) {
  ...
}

with

class ClassName extends Base1 with Trait1 with Trait2
val x = new ClassName with Trait1 with Trait2

yield

for (x <- items) yield ...
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