Appendix A. Scala cheat sheet

We use only a basic subset of Scala in the book. Here is the chronological summary of all the Scala features we use in the book. You can look at them in your editor by going to the chA_ScalaCheatSheet.scala file in the book’s code repository. Make sure to look at the README.md file, too.

Defining a value

val x: Int = 2022       
val y: String = "YYY"   
val z = true            

You can define the expected type of your value using :, but you don’t have to do it, and the compiler will try to infer it for you. Here, the inferred type of z is Boolean.

Defining a function

def f(a: Int): Int = {
  a + 1                              
}

def g(a: Int, b: String): Boolean = {
  a == b.length && z                 
}

Semicolons are not required, nor is the return keyword.

Values are ...

Get Grokking Functional Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.