2. What’s the plan? Setup development environment (Eclipse + Scalaplugin) Form pair-programming pairs Explanation of basic concepts and syntax Labs, labs, labs... ... and of course: share knowledge!
3. Euh...Scala? Scala runs on JVM and .Net VM integrates 100% with existing libraries hybrid language: both functional and OO statically typed “everything is an object” ...Immutability, Currying, Tuples, Closures, Higher Order Functions, etc....
5. Run Scala as... Interactive / REPL (Read Eval Print Loop): the scala command starts an interactive shell As script Compiled: scalac command compiles Scala code See: http://www.scala-lang.org/node/166
6. Variables, Values & Type Inference varmsg = "welcome to ..." // msg is mutable msg += " Devnology" msg = 3 // compiler error
7. Variables, Values & Type Inference valmsg = "welcome to ..." // msg is immutable msg += " Devnology" // compiler error val n : Int = 3 // explicit type declaration valname : String = "John"
8. Functions def min(x: Int, y: Int) = if (x < y) x else y // equivalent: def invert(x: Int) = -x // last statement is return value def invert(x: Int) : Int = { return –x } Unit can be considered as java’s void Watch out! def invert(x: Int) { // will return Unit, = is absent -x }
9. Every operation is a function call 1 + 2 is the same as1.+(2) “to” is not a keyword: for (i <- 0 to 10) print(i) map containsKey ‘a’ is the same as map.containsKey(‘a’)
12. For comprehensions valscores = List(1, 2, 3) for (s <- scores) println(s) for (s <- scores if s > 1) println(s)
13. Arrays valnames = Array("John", "Jane") names(0) = "Richard" // Arrays are mutable val cities = new Array[String](2) cities(0) = "Amsterdam" cities(1) = "Rotterdam" for (i <- 0 to 1) println(cities(i))
14. Maps valtowns = Map("John" -> "Amsterdam", "Jane" -> "Rotterdam") // default immutable Map var a = towns("John") // returns "Amsterdam" a = towns get "John”// returns Some(Amsterdam) a = towns get "John"get // returns "Amsterdam" var r = towns("Bill") // throws NoSuchElementException r = towns get "Bill”// returns None towns.update("John", "Delft") // returns a new Map
17. Classes & Constructors class Person(name: String, age: Int) ... val p = new Person("John", 33) val a = p.age// compiler error p.name = "Richard" // compiler error class Person(var name: String, val age: Int)
18. Companion Objects // must be declared in same file as Person class object Person { defprintName = println("John") } valp = Person // Singleton is also an object Person.printName// similar to static methods in Java/C#
21. Trait Mixin // compiler error: abstract function study from trait Student is not implemented valjohn = new Person with Student traitSimpleStudent { def greet() = "Hello amazing teacher!" } // this is ok val john = new Person withSimpleStudent println(john.greet())
22. Scala Application object Person def main(args: Array[String]) { // define a main method for (arg <- args) println("Hello " + arg) } } // or extend Application trait object Person extends Application { for (name <- List("John", "Jane")) println("Hello " + name) }
24. Case Classes case class Var(name: String) extendsExpr val v = Var("sum") adds a Factory method with the name of the class all parameters implicitly get a val prefix, so they are maintained as fields “natural” implementation of toString, hashCode and equals big advantage: support pattern matching