Scala expressions and definitions cheatsheet
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 asx
,isValid
- A
literal
, like0
,2
,abc
- A
function application
, likesqrt(x)
- An
operator application
, like-x
,x+y
- A
selection
, likemath.abs
- A
conditional expression
, likeif(x < 0) -x else z
- A
block
, like{ val x = math.abs(y); x * 2}
- An
anonymous function
, likex => 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)