SlideShare ist ein Scribd-Unternehmen logo
1 von 50
Downloaden Sie, um offline zu lesen
Scala
Simplifying Development
ScalaDay Italy
Milan, May 25, 2013
Mirco Dotta
Saturday, May 25, 13
Is Scala the Java of the
future?
Saturday, May 25, 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
Saturday, May 25, 13
How is Scala different
from Java?
Saturday, May 25, 13
Concise Syntax
Saturday, May 25, 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)
Saturday, May 25, 13
Statically typed
but feels dynamic
Saturday, May 25, 13
val x = 2
type is inferred
no semicolon
Saturday, May 25, 13
Unifies OOP and FP
Saturday, May 25, 13
Every value is an object
Saturday, May 25, 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
Saturday, May 25, 13
Everything is an
expression
Saturday, May 25, 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
Saturday, May 25, 13
Every operation is a
method call
Saturday, May 25, 13
val x: Int = 10
val y = x + 10
same as x.+(10)
Saturday, May 25, 13
Principles, not Rules
Saturday, May 25, 13
Users can write their own operators
Principle
Saturday, May 25, 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
Saturday, May 25, 13
• new types can look like built-in ones
• “grow the language”
Saturday, May 25, 13
Powerful collections
Saturday, May 25, 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)
Saturday, May 25, 13
For-comprehension
Saturday, May 25, 13
• More general than for-loops
• Used to iterate, filter, and generate new
collections
Saturday, May 25, 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
Saturday, May 25, 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))
Saturday, May 25, 13
Desugared to calls to filter, map, and
flatMap
Saturday, May 25, 13
Readily available on any class
implementing those methods!
Saturday, May 25, 13
Traits
Saturday, May 25, 13
• 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
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
Saturday, May 25, 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
Saturday, May 25, 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
Saturday, May 25, 13
Embedding DSLs
Saturday, May 25, 13
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
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
Implicit Conversions
Saturday, May 25, 13
• 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
Scala gives you the power of creating
your own conversions
Saturday, May 25, 13
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 + 2
val c1 = RichInt(1) + c //> c1 : Complex = 2 + 2
Saturday, May 25, 13
But, we want to write
val c1 = 1 + c
And not
val c1 = RichInt(1) + c
Saturday, May 25, 13
Implicit conversion!
Saturday, May 25, 13
implicit def int2richInt(n: Int) = new RichInt(n)
val c = new Complex(1, 2) //> c : Complex = 1 + 2
val 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
Scala simplifies
development because...
Saturday, May 25, 13
Less is More
Saturday, May 25, 13
Few language
constructs with high
abstraction power
Saturday, May 25, 13
It’s Fun!
Saturday, May 25, 13
Good for your
business?
Saturday, May 25, 13
Saturday, May 25, 13
Get Started in 5’
http://www.typesafe.com/platform/getstarted
Saturday, May 25, 13
Thanks
twitter: @mircodotta
Saturday, May 25, 13

Weitere ähnliche Inhalte

Mehr von Scala Italy

Mirco Dotta - Akka Streams
Mirco Dotta - Akka StreamsMirco Dotta - Akka Streams
Mirco Dotta - Akka StreamsScala Italy
 
Phil Calçado - Your microservice as a function
Phil Calçado - Your microservice as a functionPhil Calçado - Your microservice as a function
Phil Calçado - Your microservice as a functionScala Italy
 
Scalatra - Massimiliano Dessì (Energeya)
Scalatra - Massimiliano Dessì (Energeya)Scalatra - Massimiliano Dessì (Energeya)
Scalatra - Massimiliano Dessì (Energeya)Scala Italy
 
Scala: the language of languages - Mario Fusco (Red Hat)
Scala: the language of languages - Mario Fusco (Red Hat)Scala: the language of languages - Mario Fusco (Red Hat)
Scala: the language of languages - Mario Fusco (Red Hat)Scala Italy
 
Reflection in Scala Whats, Whys and Hows - Walter Cazzola (Dipartimento di In...
Reflection in Scala Whats, Whys and Hows - Walter Cazzola (Dipartimento di In...Reflection in Scala Whats, Whys and Hows - Walter Cazzola (Dipartimento di In...
Reflection in Scala Whats, Whys and Hows - Walter Cazzola (Dipartimento di In...Scala Italy
 
Simplifying development-short - Mirco Dotta (Typesafe)
Simplifying development-short - Mirco Dotta (Typesafe)Simplifying development-short - Mirco Dotta (Typesafe)
Simplifying development-short - Mirco Dotta (Typesafe)Scala Italy
 
Scala in pratica - Stefano Rocco (MoneyFarm)
Scala in pratica - Stefano Rocco (MoneyFarm)Scala in pratica - Stefano Rocco (MoneyFarm)
Scala in pratica - Stefano Rocco (MoneyFarm)Scala Italy
 

Mehr von Scala Italy (7)

Mirco Dotta - Akka Streams
Mirco Dotta - Akka StreamsMirco Dotta - Akka Streams
Mirco Dotta - Akka Streams
 
Phil Calçado - Your microservice as a function
Phil Calçado - Your microservice as a functionPhil Calçado - Your microservice as a function
Phil Calçado - Your microservice as a function
 
Scalatra - Massimiliano Dessì (Energeya)
Scalatra - Massimiliano Dessì (Energeya)Scalatra - Massimiliano Dessì (Energeya)
Scalatra - Massimiliano Dessì (Energeya)
 
Scala: the language of languages - Mario Fusco (Red Hat)
Scala: the language of languages - Mario Fusco (Red Hat)Scala: the language of languages - Mario Fusco (Red Hat)
Scala: the language of languages - Mario Fusco (Red Hat)
 
Reflection in Scala Whats, Whys and Hows - Walter Cazzola (Dipartimento di In...
Reflection in Scala Whats, Whys and Hows - Walter Cazzola (Dipartimento di In...Reflection in Scala Whats, Whys and Hows - Walter Cazzola (Dipartimento di In...
Reflection in Scala Whats, Whys and Hows - Walter Cazzola (Dipartimento di In...
 
Simplifying development-short - Mirco Dotta (Typesafe)
Simplifying development-short - Mirco Dotta (Typesafe)Simplifying development-short - Mirco Dotta (Typesafe)
Simplifying development-short - Mirco Dotta (Typesafe)
 
Scala in pratica - Stefano Rocco (MoneyFarm)
Scala in pratica - Stefano Rocco (MoneyFarm)Scala in pratica - Stefano Rocco (MoneyFarm)
Scala in pratica - Stefano Rocco (MoneyFarm)
 

Kürzlich hochgeladen

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 

Kürzlich hochgeladen (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

Simplifying development-full - Mirco Dotta (Typesafe)

  • 1. Scala Simplifying Development ScalaDay Italy Milan, May 25, 2013 Mirco Dotta Saturday, May 25, 13
  • 2. Is Scala the Java of the future? Saturday, May 25, 13
  • 3. • 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
  • 4. How is Scala different from Java? Saturday, May 25, 13
  • 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) Saturday, May 25, 13
  • 7. Statically typed but feels dynamic Saturday, May 25, 13
  • 8. val x = 2 type is inferred no semicolon Saturday, May 25, 13
  • 9. Unifies OOP and FP Saturday, May 25, 13
  • 10. Every value is an object Saturday, May 25, 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 Saturday, May 25, 13
  • 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 Saturday, May 25, 13
  • 14. Every operation is a method call Saturday, May 25, 13
  • 15. val x: Int = 10 val y = x + 10 same as x.+(10) Saturday, May 25, 13
  • 17. Users can write their own operators Principle Saturday, May 25, 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 Saturday, May 25, 13
  • 19. • new types can look like built-in ones • “grow the language” Saturday, May 25, 13
  • 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) Saturday, May 25, 13
  • 23. • More general than for-loops • Used to iterate, filter, and generate new collections Saturday, May 25, 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 Saturday, May 25, 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)) Saturday, May 25, 13
  • 26. Desugared to calls to filter, map, and flatMap Saturday, May 25, 13
  • 27. Readily available on any class implementing those methods! Saturday, May 25, 13
  • 29. • 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
  • 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 Saturday, May 25, 13
  • 31. 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
  • 32. 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
  • 34. 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
  • 35. 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
  • 37. • 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
  • 38. Scala gives you the power of creating your own conversions Saturday, May 25, 13
  • 39. 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 + 2 val c1 = RichInt(1) + c //> c1 : Complex = 2 + 2 Saturday, May 25, 13
  • 40. But, we want to write val c1 = 1 + c And not val c1 = RichInt(1) + c Saturday, May 25, 13
  • 42. implicit def int2richInt(n: Int) = new RichInt(n) val c = new Complex(1, 2) //> c : Complex = 1 + 2 val 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
  • 45. Few language constructs with high abstraction power Saturday, May 25, 13
  • 49. Get Started in 5’ http://www.typesafe.com/platform/getstarted Saturday, May 25, 13