September 2018
Intermediate to advanced
398 pages
9h 43m
English
A function or expression is said to have a side effect when it modifies some state or has some action in the outside world. For instance, printing a string to the console, writing to a file, and modifying a var, are all side effects.
In Scala, all expressions have a type. A statement which performs a side effect is of type Unit. The only value provided by the type Unit is ():
scala> val x = println("hello")hellox: Unit = ()scala> def printName(name: String): Unit = println(name)printName: (name: String)Unitscala> val y = { var a = 1 a = a+1}y: Unit = ()scala> val z = ()z: Unit = ()
A pure function is a function whose result depends only on its arguments, and that does not have any observable side effect. Scala allows you to mix ...
Read now
Unlock full access