Scala Notes
Other notes
- http://docs.scala-lang.org/cheatsheets/
t._2 // Part of a method name, such as tuple gettershttp://stackoverflow.com/a/8001132
Scala for the Impatient
Chapter 1 - Basics
valimmutable, constants. should usevalin most casesvarmutable- types of values and variables are inferred from the expression that they are initialized with, but you can specify type
- e.g.
val greeting: String = nullor val greeting: String = "Hi"
- e.g.
- everything is an object including numbers (e.g.
3.toString() - Scala compile converts primitives (
1,"hi") to wrappers - Scala has its own classes like
RichInt,RichChar,StringOps, etc that wrap Int, Char, String, etc to provide greater functionality {}.isInstanceOf[Unit]isTrue- operators
+ - * / %are methods- e.g.
a + bis equivalent toa.+(b)
- e.g.
- method calls can be written in two ways:
a method bora.method(b) - a tuple is similar to an Array however it can contain items of differing types
- tuple indexes start at 1, not 0 as in an Array
toMaptakes a collection and turns it into a Map- “static” function calls don’t look like Java’s static function calls that are called from classes
- e.g. (scala)
pow(2,4)
- e.g. (scala)
- there are no static function, instead singleton obkects
- methods without params are called without parentheses
Chapter 2
Chapter 5 - classes
- provides getter and setter methods for every field by default (so you don’t have to go crazy like one may do in Java, creating getters and setters for everything)
- getters are named after the field, and setters are named after the field with a
_appended to the end. e.g.ageandage_ - Scala will create a class for the JVM from the one below with a private age field, and public getter and setter methods (you can make them private if you declare
ageprivate)class Person { var age = 0 }
- getters are named after the field, and setters are named after the field with a
- you can also create a field using
valbut it’ll only have a getter (because it’s immutable!!), not setter