Scala expressions and definitions cheatsheet


This is a cheatsheet of Scala expressions and definitions from Coursera online course Functional Programming Principles in Scala

What is Expression

Expression is any bit of Scala code that yields a result. In other words expression evaluates to a result or results in a value

Categories of expressions

  • An identifier such as x, isValid
  • A literal, like 0, 2, abc
  • A function application, like sqrt(x)
  • An operator application, like -x, x+y
  • A selection, like math.abs
  • A conditional expression, like if(x < 0) -x else z
  • A block, like { val x = math.abs(y); x * 2}
  • An anonymous function, like x => x + 1

What is definition

Definition of something is a giving it a name and implementation.

Definition types

  • classes
  • traits
  • singleton objects
  • fields
  • methods
  • local functions
  • local variables
  • etc

Abstract members can not be defined because definition always involve some kind of implementation.

Definition may or may not contain parameters. Parameters can be:

  • call by value parameter, like (x: Int)
  • call by name parameter, like (y: => Boolean)

Alexey Bogdanov

Read more posts by this author.

Subscribe to Development notes

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!