Nick Tombeur - CGI Belgium
immutable - mutable
val name: Type = initialization // recommended var name: Type = initialization
The entire if itself is an expression. Which means it produces a result.
if (expression) { /* ... */ } else if (expression) { /* ... */ } else { /* ... */ }
A compound expression can contain any number of other expressions, including other curly-braced expressions
{ expressions }
Functions are values; methods are not.
Evaluated, every time, on call.
def name(arg1: Type1, arg2: Type2): ReturnType = { /* ... */ }
Evaluated on initialization.
lazy val will evaluate on first call; only once.
(n: Int) => { /* ... */ }
class MyClass val o = new MyClass
class Company(var name: String) class Company(val name: String) class Company(name: String) class Company(private var name: String) class Company(private val name: String) class Company { val name = "CGI" }Visibility Accessor / getter Mutator / setter var Yes Yes val Yes No Default No No private No No
// Named arguments class Company(name: String, country: String) new Company(name = "CGI", country = "Belgium") // Default arguments class Company(name: String = "CGI", country: String = "Belgium") new Company("Telenet") new Company(country = "Canada")
class Foo { def bar() = /* ... */ def bar(n: Int) = /* ... */ }
Note: overloading doesn't work in the REPL. Use REPL's :paste mode
class Foo() { println("Called on creation") }
class Point(x: Int, y: Int) { def this(x: Int) { this(x, 0) } }
case class Name(arg1: Type, arg2: Type, ...)
class Company() { /* ... */ } object Company() { /* ... */ }
more coming
// More than one within same statement import util.Random, util.Properties // Combining multiple classes in single statement import util.{Random, Properties} // Change name import util.{ Random => HelterSkelter } // Everything import util._
for (var <- list) statement for (var <- range) statement for (var <- [range/list] if condition1; if condition2; ...) statement
while (condition) { /* ... */ }
do { /* ... */ } while (condition)
High-level abstract classes/traits
// Do import scala.collection.mutable val set = mutable.Set() // Don't import scala.collection.mutable._ val set = Set()
x match { case 1 => "one" case 2 => "two" case _ => "many" }
x match { case s: String => "A String" case i: Int if i < 10 => "An Int less than 10" case i: Int => "An Int" case c: Company => "A company" }
transport match { case Car(passengers, seats) => /* ... */ case Bicycle(passengers, seats) => /* ... */ case Passenger => /* ... */ }
val company = "CGI" println(s"I am working for $company") def foo = { 1 + 1 } s"${foo}"
(element1, element2, element3, ...) val p = (1831, "Diegem") p._1 p._2 val (postal, city) = p
class Animal class Bird extends Animal class Ape extends Animal
class Animal(val age: Int) class Bird(age: Int) extends Animal(age) class Ape(age: Int, val weight: Double) extends Animal(age)
class Bar { def run = 2 } class Foo extends Bar { override def toString = "Hello World!" override def run = super.run * 2 }
object WeekDay extends Enumeration { type WeekDay = Value val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value }