Mario Fusco
The presentation is an introduction to the Scala language. It will show the most important characteristics and winning points of the language with special attention to some specific features like: the ability to painlessly mix functional and object oriented programming, its powerful collections framework, its type inference mechanism, the clear distinction between mutable and immutable data and how its actor model could make the concurrent programming more effective and less error-prone.
7. Functions and Closures
val isPositive = (x: Int) => x > 0
val numbers = List(-10, 5, 3, -2, 0, 1)
val positiveNumber = numbers.filter(isPostive)
val positiveNumber = numbers.filter(x => x > 0)
val positiveNumber = numbers.filter(_ > 0)
9. Tuples
val pair = (2, âitemsâ)
println(pair._1) // prints 2
println(pair._2) // prints items
10. Clear distinction between
mutable and immutable data
val msg = âHello,world!â // constant
var value = 3 // variable
scala.collection
scala.collection.immutable scala.collection.mutable
11. Named and default parameters
class Person(name: String = "Goofy", age: Int = 30,
location: String = "Milano")
Person(name = "Mario", age = 36, location = "Lugano")
Person(age = 36, location = "Lugano", name = "Mario")
Person(age = 36, name = "Mario")
Person(age = 36)
Person()
12. Operator overloading
class Rational (n: Int, d: Int) {
def this(n: Int) = this(n, 1)
def + (that: Rational): Rational =
new Rational(n * that.d + that.n * d, d * that.d)
def + (i: Int): Rational = new Rational(n + i * d, d)
}
13. Implicit conversion
val a = new Rational(2, 3)
val b = a + 2 // = 8/3
val c = 2 + a // Compilation Error
implicit def intToRational(x: Int) = new Rational(x)
Val c = 2 + a // = 8/3
14. Options
Tony Hoare, who invented the null reference in 1965 while
working on an object oriented language called ALGOL W, called
its invention his âbillion dollar mistakeâ
val capitals = Map("Italy" -> "Rome", "Switzerland" -> "Bern",
"Germany" -> "Berlin" , "France" -> "Paris")
println(capitals.get("Italy")) // Some(Rome)
println(capitals.get("Spain")) // None
println(capitals.get("Italy").get) // Rome
println(capitals.get("Spain").get) // thorws Exception
println(capitals.get("Spain").getOrElse("Unknown")) // Unknown
15. Traits
class Animal { def eat(): Unit }
trait Mammal extends Animal { def giveBirth(): Mammal }
trait HasWings extends Animal { def fly(): Unit }
trait HasLegs extends Animal { def walk(): Unit }
class Snake extends Animal
class Frog extends Animal with HasLegs
class Cat extends Animal with Mammal with HasLegs
class Bat extends Animal with Mammal with HasWings
class Chimera extends Animal with Mammal with HasWings with HasLegs
16. Case classes
trait Expr
case class Var(name: String) extends Expr
case class Number(num: Double) extends Expr
case class Unop(op: String, arg: Expr) extends Expr
case class Binop(op: String, l: Expr, r: Expr) extends Expr
17. Pattern matching
def simplify(expr: Expr): Expr = expr match {
case Unop("-", Unop("-", e)) => e // Double negation
case Binop("+", e, Number(0)) => e // Adding zero
case Binop("*", e, Number(1)) => e // Multiplying by one
case _ => expr
}
// Simplify double negation: simplified = Var("x")
val simplified = simplify(Unop("-", Unop("-", Var("x"))))
18. Structural Typing
(duck typing done right)
class Duck {
quack() { println "quack" }
}
doQuack(new Duck)
doQuack(d) { d.quack() }
class Dog {
barf() { println "barf" }
}
doQuack(new Dog)
def doQuack(d:{ def quack():Unit }) =
d.quack()
class Duck {
def quack() = println "quack"
}
doQuack(new Duck)
class Dog {
def barf() = println "barf"
}
doQuack(new Dog)runtime error
compilation
error
Duck typing is the dynamic mechanism that allows to discover a dog cannot
say quack only at runtime... in production ... on friday evening
19. Actors
val printerActor = actor {
loop {
receive {
case s: String => println("I got a String: " + s)
case i: Int => println("I got an Int: " + i.toString)
case _ => println(" I donât know what I got ")
}
}
}
printerActor ! "hi thereâ // prints âI got a String: hi thereâ
printerActor ! 23 // prints âI got an Int: 23â
printerActor ! 3.33 // prints âI donât know what I gotâ
22. Norbert by LinkedIn
âą Norbert is a framework written in Scala that
allows to write asynchronous, message based,
client/server applications
âą Built on Apache ZooKeeper and JBoss Netty,
Norbert to make it easy to build a cluster aware
application
âą Provides out of the box support for notifications
of cluster topology changes, application specific
routing, load balancing and partitioned workload
23. âą Akka is a framework that allows to write simpler
concurrent (yet correct) applications
âą It provides a higher level of abstraction for
writing concurrent and distributed systems
through (remote) actors
âą It implements Software Transactional Memory
(STM) turning the Java heap into a transactional
data set with begin/commit/rollback semantic
âą Fault-tolerant adopting the "Let it crash" /
"Embrace failure" model
24. SBT (simple-build-tool)
âą Sbt is a build tool written in Scala
âą Uses the same directory structure as
Maven for source files
âą Uses Ivy to resolve dependencies
âą Compatible with Maven configuration files
âą Supports testing with ScalaTest
âą Parallel task execution, including parallel
test execution