SlideShare a Scribd company logo
Scala:	
  Case	
  Classes	
  
Let's	
  model	
  the	
  following	
  
•  A	
  product	
  has	
  a	
  name,	
  category	
  and	
  an	
  
op;onal	
  descrip;on	
  
•  A	
  category	
  has	
  a	
  name	
  and	
  may	
  have	
  a	
  parent	
  
category	
  
•  All	
  the	
  above	
  a>ributes	
  are	
  read-­‐only	
  
public class Category {

private String name;
private Category parent;

public Category(String name, Category parent) {
this.name = name;
this.parent = parent;
}

public String getName() {
return name;
}

public Category getParent() {
return parent;
}
}
public class Product {

private String name;
private String description;
private Category category;

// TODO: What am I missing?
public Product(String name, String description, Category category) {
this.name = name;
this.description = description;
this.category = category;
}

public String getName() {
return name;
}

public String getDescription() {
return description;
}

public Category getCategory() {
return category;
}
}
And	
  in	
  Scala?	
  
class Category(
val name: String, 
val parent: Option[Category])

class Product(
val name: String, 
val description: String, 
val category: Category)
Or	
  even	
  be>er,	
  
case class Category(
name: String, 
parent: Option[Category])

case class Product(
name: String, 
description: String, 
category: Category)
What	
  you	
  get	
  for	
  free	
  
•  Companion	
  object	
  with	
  factory-­‐like	
  apply	
  
methods	
  
•  Class	
  parameters	
  become	
  fields	
  
•  Nicer	
  toString	
  
•  A>ribute	
  based	
  equality	
  
•  Pa>ern	
  matching	
  
•  Copy	
  method	
  to	
  'clone'	
  instances	
  
	
  
Be	
  mindful	
  of	
  
•  Bytecode	
  overhead	
  
•  Generated	
  code	
  might	
  clu>er	
  your	
  interface	
  
•  Case	
  class	
  inheritance	
  
Demonstrating Case Classes in Scala

More Related Content

Viewers also liked (15)

ODP
Case class scala
Matt Hicks
 
PDF
Pattern Matching in Scala
Derek Chen-Becker
 
PDF
Intro to pattern matching in scala
Jan Krag
 
PDF
Run-Away-Bear (1984)
James Beers
 
PDF
Finishing brands atomization technology - vietnamese
Vu Nguyen
 
PPTX
A Good Designed Website Relieves Your Business
wemakedesignsmkt
 
PDF
James D. Beers CV (5-2-16)
James Beers
 
PPTX
Ferdigheter for en nav veileder
unnihelene50
 
PPTX
One stop english
Jazmin Ontiveros Ontiveros
 
PDF
Corporate intro presentation
Vu Nguyen
 
PDF
Introduction to the Typesafe Reactive Platform
BoldRadius Solutions
 
PPT
Banner locations of Pragyan
Vishnu Gopal
 
PPTX
Corner Jetted Bathtubs
Aquaticabath
 
PDF
6 benso, alban. - da micro-historia
Milaysa Paz
 
PPTX
Elektriskie lādiņi
Zigeta Simanoviča
 
Case class scala
Matt Hicks
 
Pattern Matching in Scala
Derek Chen-Becker
 
Intro to pattern matching in scala
Jan Krag
 
Run-Away-Bear (1984)
James Beers
 
Finishing brands atomization technology - vietnamese
Vu Nguyen
 
A Good Designed Website Relieves Your Business
wemakedesignsmkt
 
James D. Beers CV (5-2-16)
James Beers
 
Ferdigheter for en nav veileder
unnihelene50
 
One stop english
Jazmin Ontiveros Ontiveros
 
Corporate intro presentation
Vu Nguyen
 
Introduction to the Typesafe Reactive Platform
BoldRadius Solutions
 
Banner locations of Pragyan
Vishnu Gopal
 
Corner Jetted Bathtubs
Aquaticabath
 
6 benso, alban. - da micro-historia
Milaysa Paz
 
Elektriskie lādiņi
Zigeta Simanoviča
 

Similar to Demonstrating Case Classes in Scala (12)

PPTX
Joy of scala
Maxim Novak
 
PPTX
Overview of ECE Engineer
piyushkumar979747
 
PPTX
Oop2010 Scala Presentation Stal
Michael Stal
 
PDF
Scala101, first steps with Scala
Giampaolo Trapasso
 
PPTX
Qcon2011 functions rockpresentation_scala
Michael Stal
 
PPTX
Scala case of case classes
VulcanMinds
 
PDF
Programming in scala - 1
Mukesh Kumar
 
ODP
Naïveté vs. Experience
Mike Fogus
 
DOCX
OOP Lab Report.docx
ArafatSahinAfridi
 
PDF
Scala: A brief tutorial
Oliver Szymanski
 
PPTX
Java session2
Rajeev Kumar
 
PDF
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Philip Schwarz
 
Joy of scala
Maxim Novak
 
Overview of ECE Engineer
piyushkumar979747
 
Oop2010 Scala Presentation Stal
Michael Stal
 
Scala101, first steps with Scala
Giampaolo Trapasso
 
Qcon2011 functions rockpresentation_scala
Michael Stal
 
Scala case of case classes
VulcanMinds
 
Programming in scala - 1
Mukesh Kumar
 
Naïveté vs. Experience
Mike Fogus
 
OOP Lab Report.docx
ArafatSahinAfridi
 
Scala: A brief tutorial
Oliver Szymanski
 
Java session2
Rajeev Kumar
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Philip Schwarz
 
Ad

More from BoldRadius Solutions (18)

PDF
Partial Functions in Scala
BoldRadius Solutions
 
PDF
Towards Reliable Lookups - Scala By The Bay
BoldRadius Solutions
 
PDF
How You Convince Your Manager To Adopt Scala.js in Production
BoldRadius Solutions
 
PDF
Introduction to the Actor Model
BoldRadius Solutions
 
PDF
Why Not Make the Transition from Java to Scala?
BoldRadius Solutions
 
PDF
String Interpolation in Scala | BoldRadius
BoldRadius Solutions
 
PPTX
Value Classes in Scala | BoldRadius
BoldRadius Solutions
 
PDF
Scala Days Highlights | BoldRadius
BoldRadius Solutions
 
PDF
What Are For Expressions in Scala?
BoldRadius Solutions
 
PPTX
Domain Driven Design Through Onion Architecture
BoldRadius Solutions
 
PDF
Pattern Matching in Scala
BoldRadius Solutions
 
PDF
What are Sealed Classes in Scala?
BoldRadius Solutions
 
PDF
Scala: Collections API
BoldRadius Solutions
 
PDF
How To Use Higher Order Functions in Scala
BoldRadius Solutions
 
PDF
Immutability in Scala
BoldRadius Solutions
 
PDF
Scala Days 2014: Pitching Typesafe
BoldRadius Solutions
 
PDF
Code Brevity in Scala
BoldRadius Solutions
 
PDF
Functional Programming - Worth the Effort
BoldRadius Solutions
 
Partial Functions in Scala
BoldRadius Solutions
 
Towards Reliable Lookups - Scala By The Bay
BoldRadius Solutions
 
How You Convince Your Manager To Adopt Scala.js in Production
BoldRadius Solutions
 
Introduction to the Actor Model
BoldRadius Solutions
 
Why Not Make the Transition from Java to Scala?
BoldRadius Solutions
 
String Interpolation in Scala | BoldRadius
BoldRadius Solutions
 
Value Classes in Scala | BoldRadius
BoldRadius Solutions
 
Scala Days Highlights | BoldRadius
BoldRadius Solutions
 
What Are For Expressions in Scala?
BoldRadius Solutions
 
Domain Driven Design Through Onion Architecture
BoldRadius Solutions
 
Pattern Matching in Scala
BoldRadius Solutions
 
What are Sealed Classes in Scala?
BoldRadius Solutions
 
Scala: Collections API
BoldRadius Solutions
 
How To Use Higher Order Functions in Scala
BoldRadius Solutions
 
Immutability in Scala
BoldRadius Solutions
 
Scala Days 2014: Pitching Typesafe
BoldRadius Solutions
 
Code Brevity in Scala
BoldRadius Solutions
 
Functional Programming - Worth the Effort
BoldRadius Solutions
 
Ad

Recently uploaded (20)

DOCX
Import Data Form Excel to Tally Services
Tally xperts
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPT
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Import Data Form Excel to Tally Services
Tally xperts
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 

Demonstrating Case Classes in Scala

  • 2. Let's  model  the  following   •  A  product  has  a  name,  category  and  an   op;onal  descrip;on   •  A  category  has  a  name  and  may  have  a  parent   category   •  All  the  above  a>ributes  are  read-­‐only  
  • 3. public class Category { private String name; private Category parent; public Category(String name, Category parent) { this.name = name; this.parent = parent; } public String getName() { return name; } public Category getParent() { return parent; } }
  • 4. public class Product { private String name; private String description; private Category category; // TODO: What am I missing? public Product(String name, String description, Category category) { this.name = name; this.description = description; this.category = category; } public String getName() { return name; } public String getDescription() { return description; } public Category getCategory() { return category; } }
  • 5. And  in  Scala?   class Category( val name: String, val parent: Option[Category]) class Product( val name: String, val description: String, val category: Category)
  • 6. Or  even  be>er,   case class Category( name: String, parent: Option[Category]) case class Product( name: String, description: String, category: Category)
  • 7. What  you  get  for  free   •  Companion  object  with  factory-­‐like  apply   methods   •  Class  parameters  become  fields   •  Nicer  toString   •  A>ribute  based  equality   •  Pa>ern  matching   •  Copy  method  to  'clone'  instances    
  • 8. Be  mindful  of   •  Bytecode  overhead   •  Generated  code  might  clu>er  your  interface   •  Case  class  inheritance