simplifying development-full - mirco dotta (typesafe)
DESCRIPTION
Scala meetup - Milan, 25 May 2013 Una delle caratteristiche peculiari di Scala consiste nel semplificare e velocizzare la fase di sviluppo del software. Unificando la programmazione orientata agli oggetti con la programmazione funzionale, Scala permette di esprimervi in maniera concisa ed efficace. In questa presentazione saranno introdotte le caratteristiche principali del linguaggio e la sua filosofia, al fine di mostrare come del codice scritto in Scala risulti più semplice, corretto e manutenibile.TRANSCRIPT
![Page 1: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/1.jpg)
ScalaSimplifying Development
ScalaDay ItalyMilan, May 25, 2013
Mirco Dotta
Saturday, May 25, 13
![Page 2: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/2.jpg)
Is Scala the Java of the future?
Saturday, May 25, 13
![Page 3: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/3.jpg)
• 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
Saturday, May 25, 13
![Page 4: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/4.jpg)
How is Scala different from Java?
Saturday, May 25, 13
![Page 5: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/5.jpg)
Concise SyntaxSaturday, May 25, 13
![Page 6: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/6.jpg)
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)
Saturday, May 25, 13
![Page 7: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/7.jpg)
Statically typedbut feels dynamic
Saturday, May 25, 13
![Page 8: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/8.jpg)
val x = 2type is inferred
no semicolon
Saturday, May 25, 13
![Page 9: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/9.jpg)
Unifies OOP and FPSaturday, May 25, 13
![Page 10: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/10.jpg)
Every value is an object
Saturday, May 25, 13
![Page 11: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/11.jpg)
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
Saturday, May 25, 13
![Page 12: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/12.jpg)
Everything is an expression
Saturday, May 25, 13
![Page 13: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/13.jpg)
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
Saturday, May 25, 13
![Page 14: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/14.jpg)
Every operation is a method call
Saturday, May 25, 13
![Page 15: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/15.jpg)
val x: Int = 10val y = x + 10
same as x.+(10)
Saturday, May 25, 13
![Page 16: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/16.jpg)
Principles, not Rules
Saturday, May 25, 13
![Page 17: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/17.jpg)
Users can write their own operators
Principle
Saturday, May 25, 13
![Page 18: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/18.jpg)
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 + 2val c2 = new Complex(2, 2) //> c2 : Complex = 2 + 2val c = c1 + c2 //> c : Complex = 3 + 4
default argument
Saturday, May 25, 13
![Page 19: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/19.jpg)
• new types can look like built-in ones
• “grow the language”
Saturday, May 25, 13
![Page 20: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/20.jpg)
Powerful collectionsSaturday, May 25, 13
![Page 21: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/21.jpg)
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)
Saturday, May 25, 13
![Page 22: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/22.jpg)
For-comprehension
Saturday, May 25, 13
![Page 23: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/23.jpg)
• More general than for-loops
• Used to iterate, filter, and generate new collections
Saturday, May 25, 13
![Page 24: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/24.jpg)
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
Saturday, May 25, 13
![Page 25: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/25.jpg)
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))
Saturday, May 25, 13
![Page 26: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/26.jpg)
Desugared to calls to filter, map, and flatMap
Saturday, May 25, 13
![Page 27: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/27.jpg)
Readily available on any class implementing those methods!
Saturday, May 25, 13
![Page 28: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/28.jpg)
TraitsSaturday, May 25, 13
![Page 29: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/29.jpg)
• Like Java interfaces, but traits
• can have behavior (like Java 8 interfaces with defender methods)
• can have state
• enable multiple inheritance
Saturday, May 25, 13
![Page 30: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/30.jpg)
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
InterfaceSaturday, May 25, 13
![Page 31: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/31.jpg)
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
Saturday, May 25, 13
![Page 32: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/32.jpg)
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
Saturday, May 25, 13
![Page 33: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/33.jpg)
Embedding DSLsSaturday, May 25, 13
![Page 34: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/34.jpg)
val c = new Complex(1, 2) //> c : Complex = 1 + 2
Sum Complex & Int
How could we do it?
val c1 = 1 + c //> ???
val c1 = c + 1 //> ???
Saturday, May 25, 13
![Page 35: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/35.jpg)
val c1 = 1 + c //> ???
• This doesn’t compile because the type of c is not conform to the type expected by the + method
• In Java there would simply be no way to make this work
Saturday, May 25, 13
![Page 36: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/36.jpg)
Implicit ConversionsSaturday, May 25, 13
![Page 37: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/37.jpg)
• When there is a type error, the compiler looks for an implicit that could heal the expression
• You are already used to the idea of types being automatically converted into others
• E.g., Type coercion in Java!
int a = 2;double b = a;
int is converted into a double
Saturday, May 25, 13
![Page 38: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/38.jpg)
Scala gives you the power of creating your own conversions
Saturday, May 25, 13
![Page 39: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/39.jpg)
class RichInt(n: Int) { def +(other: Complex) = new Complex(n) + other}
Let’s create a class that can sum Int with Complex
val c = new Complex(1, 2) //> c : Complex = 1 + 2val c1 = RichInt(1) + c //> c1 : Complex = 2 + 2
Saturday, May 25, 13
![Page 40: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/40.jpg)
But, we want to write
val c1 = 1 + c
And not
val c1 = RichInt(1) + c
Saturday, May 25, 13
![Page 41: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/41.jpg)
Implicit conversion!Saturday, May 25, 13
![Page 42: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/42.jpg)
implicit def int2richInt(n: Int) = new RichInt(n)
val c = new Complex(1, 2) //> c : Complex = 1 + 2val c1 = 1 + c //> c1 : Complex = 2 + 2
And the compiler will take care of applying the conversion
val c1 = int2richInt(1) + c
Saturday, May 25, 13
![Page 43: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/43.jpg)
Scala simplifies development because...
Saturday, May 25, 13
![Page 44: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/44.jpg)
Less is More
Saturday, May 25, 13
![Page 45: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/45.jpg)
Few language constructs with high abstraction power
Saturday, May 25, 13
![Page 46: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/46.jpg)
It’s Fun!
Saturday, May 25, 13
![Page 47: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/47.jpg)
Good for your business?
Saturday, May 25, 13
![Page 48: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/48.jpg)
Saturday, May 25, 13
![Page 49: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/49.jpg)
Get Started in 5’
http://www.typesafe.com/platform/getstarted
Saturday, May 25, 13
![Page 50: Simplifying development-full - Mirco Dotta (Typesafe)](https://reader034.vdocuments.pub/reader034/viewer/2022051816/5458c7b5b1af9f37608b5405/html5/thumbnails/50.jpg)
Thankstwitter: @mircodotta
Saturday, May 25, 13