SlideShare a Scribd company logo
Pa$ern 
Matching 
in 
Scala
First, 
an 
example 
• Users 
have 
accounts 
• Accounts 
are 
either 
Paypal 
or 
Bitcoin 
• Paypal 
accounts 
have 
an 
email 
• Bitcoin 
accounts 
have 
a 
key
case class User(name: String, account: Account) 
sealed class Account 
case class Paypal(email: String) extends Account 
case class Bitcoin(key: String) extends Account
Iden@fy 
account 
type 
private String whatis(Object obj) { 
if(obj instanceof User) { 
User user = (User) obj; 
if(user.account instanceof Paypal) { 
Paypal paypal = (Paypal) user.account; 
return "Paypal for email " + paypal.email; 
} else if(user.account instanceof Bitcoin) { 
Bitcoin bitcoin = (Bitcoin) user.account; 
return "Bitcoin with key " + bitcoin.key; 
} else { 
return "Unknown account type" 
} 
} else { 
return "Unknown object type"; 
} 
}
Iden@fy 
account 
type 
def whatis(obj: Any) { 
obj match { 
case User(name, Paypal(email)) => 
"Paypal for email " + email 
case User(name, Bitcoin(key)) => 
"Bitcoin with key " + key 
case _ => "Unknown object type" 
} 
}
Lots 
more 
power 
def whatis(obj: Any) { 
obj match { 
case User(name, Paypal("bilbo@shire.com")) 
if(!name.isEmpty) => 
"Bilbo's got Paypal" 
... 
} 
}
Pa$erns 
on 
val 
crea@on 
def getTuple: (User, Account) = ... 
// Decompose the tuple, no need for _1 and _2 
accessors 
val (user,account) = getTuple
Pattern Matching in Scala

More Related Content

Viewers also liked (20)

PDF
Functional Programming - Worth the Effort
BoldRadius Solutions
 
PPTX
Curriculum
thipik
 
PPTX
Test
DDgmorrison1
 
PPTX
Paul Partlow Highlighted Portfolio
Paul Partlow
 
PPTX
Value Classes in Scala | BoldRadius
BoldRadius Solutions
 
PDF
Scala: Collections API
BoldRadius Solutions
 
PDF
Empirics of standard deviation
Adebanji Ayeni
 
PDF
Taste of korea
Marta Garcia Trincado
 
PDF
Code Brevity in Scala
BoldRadius Solutions
 
PDF
What Are For Expressions in Scala?
BoldRadius Solutions
 
PPT
Earth moon 1
TrapQveen Mia
 
PDF
Scala Days Highlights | BoldRadius
BoldRadius Solutions
 
PDF
Punishment Driven Development #agileinthecity
Louise Elliott
 
PDF
Why Not Make the Transition from Java to Scala?
BoldRadius Solutions
 
PDF
Immutability in Scala
BoldRadius Solutions
 
PDF
How You Convince Your Manager To Adopt Scala.js in Production
BoldRadius Solutions
 
PDF
scientific method and the research process
Adebanji Ayeni
 
PPTX
GSLV MARK 3
APOORVA
 
PPTX
Domain Driven Design Through Onion Architecture
BoldRadius Solutions
 
PPTX
Haptens
Sonika Bhatnagar
 
Functional Programming - Worth the Effort
BoldRadius Solutions
 
Curriculum
thipik
 
Paul Partlow Highlighted Portfolio
Paul Partlow
 
Value Classes in Scala | BoldRadius
BoldRadius Solutions
 
Scala: Collections API
BoldRadius Solutions
 
Empirics of standard deviation
Adebanji Ayeni
 
Taste of korea
Marta Garcia Trincado
 
Code Brevity in Scala
BoldRadius Solutions
 
What Are For Expressions in Scala?
BoldRadius Solutions
 
Earth moon 1
TrapQveen Mia
 
Scala Days Highlights | BoldRadius
BoldRadius Solutions
 
Punishment Driven Development #agileinthecity
Louise Elliott
 
Why Not Make the Transition from Java to Scala?
BoldRadius Solutions
 
Immutability in Scala
BoldRadius Solutions
 
How You Convince Your Manager To Adopt Scala.js in Production
BoldRadius Solutions
 
scientific method and the research process
Adebanji Ayeni
 
GSLV MARK 3
APOORVA
 
Domain Driven Design Through Onion Architecture
BoldRadius Solutions
 

More from BoldRadius Solutions (7)

PDF
Introduction to the Typesafe Reactive Platform
BoldRadius Solutions
 
PDF
Towards Reliable Lookups - Scala By The Bay
BoldRadius Solutions
 
PDF
Introduction to the Actor Model
BoldRadius Solutions
 
PDF
What are Sealed Classes in Scala?
BoldRadius Solutions
 
PDF
How To Use Higher Order Functions in Scala
BoldRadius Solutions
 
PDF
Scala Days 2014: Pitching Typesafe
BoldRadius Solutions
 
PDF
Demonstrating Case Classes in Scala
BoldRadius Solutions
 
Introduction to the Typesafe Reactive Platform
BoldRadius Solutions
 
Towards Reliable Lookups - Scala By The Bay
BoldRadius Solutions
 
Introduction to the Actor Model
BoldRadius Solutions
 
What are Sealed Classes in Scala?
BoldRadius Solutions
 
How To Use Higher Order Functions in Scala
BoldRadius Solutions
 
Scala Days 2014: Pitching Typesafe
BoldRadius Solutions
 
Demonstrating Case Classes in Scala
BoldRadius Solutions
 
Ad

Pattern Matching in Scala

  • 2. First, an example • Users have accounts • Accounts are either Paypal or Bitcoin • Paypal accounts have an email • Bitcoin accounts have a key
  • 3. case class User(name: String, account: Account) sealed class Account case class Paypal(email: String) extends Account case class Bitcoin(key: String) extends Account
  • 4. Iden@fy account type private String whatis(Object obj) { if(obj instanceof User) { User user = (User) obj; if(user.account instanceof Paypal) { Paypal paypal = (Paypal) user.account; return "Paypal for email " + paypal.email; } else if(user.account instanceof Bitcoin) { Bitcoin bitcoin = (Bitcoin) user.account; return "Bitcoin with key " + bitcoin.key; } else { return "Unknown account type" } } else { return "Unknown object type"; } }
  • 5. Iden@fy account type def whatis(obj: Any) { obj match { case User(name, Paypal(email)) => "Paypal for email " + email case User(name, Bitcoin(key)) => "Bitcoin with key " + key case _ => "Unknown object type" } }
  • 6. Lots more power def whatis(obj: Any) { obj match { case User(name, Paypal("[email protected]")) if(!name.isEmpty) => "Bilbo's got Paypal" ... } }
  • 7. Pa$erns on val crea@on def getTuple: (User, Account) = ... // Decompose the tuple, no need for _1 and _2 accessors val (user,account) = getTuple