Development notes

Thoughts, notes and ideas about development

Scala expressions and definitions cheatsheet

2017-09-13 1 min read Development Alexey Bogdanov

Intro

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)
comments powered by Disqus