Scala
Simplifying Development
ScalaDay Italy
Milan, May 25, 2013
Mirco Dotta
Sunday, May 26, 13
Is Scala the Java of the
future?
1/38
Sunday, May 26, 13
• It has basically everything Java has now
• It has closures (planned for Java 8)
• It has rich interfaces (Java 8 defender
methods), and more
• It is completely interoperable and runs
about as fast as Java
2/38
Sunday, May 26, 13
How is Scala different
from Java?
3/38
Sunday, May 26, 13
Concise Syntax
4/38
Sunday, May 26, 13
public class Time {
private final int hours;
private final int minutes;
public Time(int hours, int minutes) {
this.hours = hours;
this.minutes = minutes;
}
public int getHours() { return hours; }
public int getMinutes() { return minutes; }
}
class Time(val hours: Int, val minutes: Int)
5/38
Sunday, May 26, 13
Statically typed
but feels dynamic
6/38
Sunday, May 26, 13
val x = 2
type is inferred
no semicolon
7/38
Sunday, May 26, 13
Unifies OOP and FP
8/38
Sunday, May 26, 13
Every value is an object
9/38
Sunday, May 26, 13
List(1,2,3).filter(x => x > 2) //> res: List[Int] = List(3)
Anonymous function Int => Boolean
List(1, 2, 3).filter(
new Function[Int, Boolean] {
def apply(x: Int): Boolean = x > 2
}
)
/
//> res: List[Int] = List(3)
Functions are “just” objects
10/38
Sunday, May 26, 13
Everything is an
expression
11/38
Sunday, May 26, 13
def max(x: Int, y: Int): Int =
if (x > y) x else y
no return statement
val x: Int = {
val y = 10
val z = 5
y + z
}
blocks evaluate to last
expression
12/38
Sunday, May 26, 13
Every operation is a
method call
13/38
Sunday, May 26, 13
val x: Int = 10
val y = x + 10
same as x.+(10)
14/38
Sunday, May 26, 13
Principles, not Rules
15/38
Sunday, May 26, 13
Users can write their own operators
16/38
Principle
Sunday, May 26, 13
class Complex(val re: Int, val im: Int = 0) {
def +(that: Complex) =
new Complex(this.re + that.re, this.im + that.im)
override def toString = s"$re + $im"
}
val c1 = new Complex(1, 2) //> c1 : Complex = 1 + 2
val c2 = new Complex(2, 2) //> c2 : Complex = 2 + 2
val c = c1 + c2 //> c : Complex = 3 + 4
default argument
17/38
Sunday, May 26, 13
• new types can look like built-in ones
• “grow the language”
Sunday, May 26, 13
Powerful collections
18/38
Sunday, May 26, 13
case class Time(hours: Int, minutes: Int = 0)
val times = List(Time(1), Time(2), Time(12,30), Time(16,15))
times.filter(time => time.hours >= 12)
//> res: List[Time] = List(Time(12,30), Time(16,15))
times.map(time => Time(time.hours + 1, time.minutes))
//> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30),
Time(17,15))
times.take(2) //> res: List[Time] = List(Time(1,0), Time(2,0))
times.groupBy(time => time.minutes) //> res: Map[Int,List[Time]] =
Map(30 -> List(Time(12,30)), 15 -> List(Time(16,15)), 0 ->
List(Time(1,0), Time(2,0)))
times.head //> res: Time = Time(1,0)
times.last //> res: Time = Time(16,15)
19/38
Sunday, May 26, 13
For-comprehension
20/38
Sunday, May 26, 13
• More general than for-loops
• Used to iterate, filter, and generate new
collections
21/38
Sunday, May 26, 13
for (p <- persons; pr <- p.projects;
if pr.overdue) yield p.name
may have any number of generators
guard construct a new collection of the same
type, element by element
p is in scope for other generators
22/38
Sunday, May 26, 13
times.filter(time => time.hours >= 12)
//> res: List[Time] = List(Time(12,30), Time(16,15))
for(time <- times;
if time.hours >= 12) yield time
//>res: List[Time] = List(Time(12,30), Time(16,15))
times.map(time => Time(time.hours + 1, time.minutes))
//> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30),
Time(17,15))
for(time <- times)
yield Time(time.hours + 1, time.minutes)
//> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30),
Time(17,15))
23/38
Sunday, May 26, 13
Desugared to calls to filter, map, and
flatMap
24/38
Sunday, May 26, 13
Readily available on any class
implementing those methods!
25/38
Sunday, May 26, 13
Traits
26/38
Sunday, May 26, 13
• Like Java interfaces, but traits
• can have behavior (like Java 8 interfaces
with defender methods)
• can have state
• enable multiple inheritance
27/38
Sunday, May 26, 13
public interface Comparable<T> {
int compareTo(int o);
}
trait Comparable[T] {
def compareTo(that: T): Int
}
def <(that: T): Boolean =
(this compare that) < 0
def >(that: T): Boolean =
(this compare that) > 0
//... Rich
Interface
28/38
Sunday, May 26, 13
Multiple Inheritance
• Traits can mix-in multiple traits
• Classes can mix-in multiple traits
• Both Class and Trait can inherit at most
from one Class
29/38
Sunday, May 26, 13
trait Bird {
def fly: String = "I'm flying!"
}
trait Swimmer {
def swim: String = "I'm swimming!"
}
class Fish extends Swimmer
class Duck extends Bird with Swimmer
30/38
Sunday, May 26, 13
Embedding DSLs
Sunday, May 26, 13
Scala simplifies
development because...
32/38
Sunday, May 26, 13
Less is More
33/38
Sunday, May 26, 13
Few language
constructs with high
abstraction power
34/38
Sunday, May 26, 13
It’s Fun!
35/38
Sunday, May 26, 13
Good for your
business?
36/38
Sunday, May 26, 13
37/38
Sunday, May 26, 13
Get Started in 5’
38/38
http://www.typesafe.com/platform/getstarted
Sunday, May 26, 13
Thanks
twitter: @mircodotta
Sunday, May 26, 13

Simplifying development-short - Mirco Dotta (Typesafe)

  • 1.
    Scala Simplifying Development ScalaDay Italy Milan,May 25, 2013 Mirco Dotta Sunday, May 26, 13
  • 2.
    Is Scala theJava of the future? 1/38 Sunday, May 26, 13
  • 3.
    • It hasbasically everything Java has now • It has closures (planned for Java 8) • It has rich interfaces (Java 8 defender methods), and more • It is completely interoperable and runs about as fast as Java 2/38 Sunday, May 26, 13
  • 4.
    How is Scaladifferent from Java? 3/38 Sunday, May 26, 13
  • 5.
  • 6.
    public class Time{ private final int hours; private final int minutes; public Time(int hours, int minutes) { this.hours = hours; this.minutes = minutes; } public int getHours() { return hours; } public int getMinutes() { return minutes; } } class Time(val hours: Int, val minutes: Int) 5/38 Sunday, May 26, 13
  • 7.
    Statically typed but feelsdynamic 6/38 Sunday, May 26, 13
  • 8.
    val x =2 type is inferred no semicolon 7/38 Sunday, May 26, 13
  • 9.
    Unifies OOP andFP 8/38 Sunday, May 26, 13
  • 10.
    Every value isan object 9/38 Sunday, May 26, 13
  • 11.
    List(1,2,3).filter(x => x> 2) //> res: List[Int] = List(3) Anonymous function Int => Boolean List(1, 2, 3).filter( new Function[Int, Boolean] { def apply(x: Int): Boolean = x > 2 } ) / //> res: List[Int] = List(3) Functions are “just” objects 10/38 Sunday, May 26, 13
  • 12.
  • 13.
    def max(x: Int,y: Int): Int = if (x > y) x else y no return statement val x: Int = { val y = 10 val z = 5 y + z } blocks evaluate to last expression 12/38 Sunday, May 26, 13
  • 14.
    Every operation isa method call 13/38 Sunday, May 26, 13
  • 15.
    val x: Int= 10 val y = x + 10 same as x.+(10) 14/38 Sunday, May 26, 13
  • 16.
  • 17.
    Users can writetheir own operators 16/38 Principle Sunday, May 26, 13
  • 18.
    class Complex(val re:Int, val im: Int = 0) { def +(that: Complex) = new Complex(this.re + that.re, this.im + that.im) override def toString = s"$re + $im" } val c1 = new Complex(1, 2) //> c1 : Complex = 1 + 2 val c2 = new Complex(2, 2) //> c2 : Complex = 2 + 2 val c = c1 + c2 //> c : Complex = 3 + 4 default argument 17/38 Sunday, May 26, 13
  • 19.
    • new typescan look like built-in ones • “grow the language” Sunday, May 26, 13
  • 20.
  • 21.
    case class Time(hours:Int, minutes: Int = 0) val times = List(Time(1), Time(2), Time(12,30), Time(16,15)) times.filter(time => time.hours >= 12) //> res: List[Time] = List(Time(12,30), Time(16,15)) times.map(time => Time(time.hours + 1, time.minutes)) //> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30), Time(17,15)) times.take(2) //> res: List[Time] = List(Time(1,0), Time(2,0)) times.groupBy(time => time.minutes) //> res: Map[Int,List[Time]] = Map(30 -> List(Time(12,30)), 15 -> List(Time(16,15)), 0 -> List(Time(1,0), Time(2,0))) times.head //> res: Time = Time(1,0) times.last //> res: Time = Time(16,15) 19/38 Sunday, May 26, 13
  • 22.
  • 23.
    • More generalthan for-loops • Used to iterate, filter, and generate new collections 21/38 Sunday, May 26, 13
  • 24.
    for (p <-persons; pr <- p.projects; if pr.overdue) yield p.name may have any number of generators guard construct a new collection of the same type, element by element p is in scope for other generators 22/38 Sunday, May 26, 13
  • 25.
    times.filter(time => time.hours>= 12) //> res: List[Time] = List(Time(12,30), Time(16,15)) for(time <- times; if time.hours >= 12) yield time //>res: List[Time] = List(Time(12,30), Time(16,15)) times.map(time => Time(time.hours + 1, time.minutes)) //> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30), Time(17,15)) for(time <- times) yield Time(time.hours + 1, time.minutes) //> res: List[Time] = List(Time(2,0), Time(3,0), Time(13,30), Time(17,15)) 23/38 Sunday, May 26, 13
  • 26.
    Desugared to callsto filter, map, and flatMap 24/38 Sunday, May 26, 13
  • 27.
    Readily available onany class implementing those methods! 25/38 Sunday, May 26, 13
  • 28.
  • 29.
    • Like Javainterfaces, but traits • can have behavior (like Java 8 interfaces with defender methods) • can have state • enable multiple inheritance 27/38 Sunday, May 26, 13
  • 30.
    public interface Comparable<T>{ int compareTo(int o); } trait Comparable[T] { def compareTo(that: T): Int } def <(that: T): Boolean = (this compare that) < 0 def >(that: T): Boolean = (this compare that) > 0 //... Rich Interface 28/38 Sunday, May 26, 13
  • 31.
    Multiple Inheritance • Traitscan mix-in multiple traits • Classes can mix-in multiple traits • Both Class and Trait can inherit at most from one Class 29/38 Sunday, May 26, 13
  • 32.
    trait Bird { deffly: String = "I'm flying!" } trait Swimmer { def swim: String = "I'm swimming!" } class Fish extends Swimmer class Duck extends Bird with Swimmer 30/38 Sunday, May 26, 13
  • 33.
  • 34.
  • 35.
  • 36.
    Few language constructs withhigh abstraction power 34/38 Sunday, May 26, 13
  • 37.
  • 38.
  • 39.
  • 40.
    Get Started in5’ 38/38 http://www.typesafe.com/platform/getstarted Sunday, May 26, 13
  • 41.