SlideShare a Scribd company logo
1 of 9
Download to read offline
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

Case class scala
Case class scalaCase class scala
Case class scalaMatt Hicks
 
Intro to pattern matching in scala
Intro to pattern matching in scalaIntro to pattern matching in scala
Intro to pattern matching in scalaJan Krag
 
Run-Away-Bear (1984)
Run-Away-Bear (1984)Run-Away-Bear (1984)
Run-Away-Bear (1984)James Beers
 
Finishing brands atomization technology - vietnamese
Finishing brands   atomization technology - vietnameseFinishing brands   atomization technology - vietnamese
Finishing brands atomization technology - vietnameseVu Nguyen
 
A Good Designed Website Relieves Your Business
A Good Designed Website Relieves Your BusinessA Good Designed Website Relieves Your Business
A Good Designed Website Relieves Your Businesswemakedesignsmkt
 
James D. Beers CV (5-2-16)
James D. Beers CV (5-2-16)James D. Beers CV (5-2-16)
James D. Beers CV (5-2-16)James Beers
 
Ferdigheter for en nav veileder
Ferdigheter for en nav  veilederFerdigheter for en nav  veileder
Ferdigheter for en nav veilederunnihelene50
 
Corporate intro presentation
Corporate intro presentationCorporate intro presentation
Corporate intro presentationVu Nguyen
 
Introduction to the Typesafe Reactive Platform
Introduction to the Typesafe Reactive PlatformIntroduction to the Typesafe Reactive Platform
Introduction to the Typesafe Reactive PlatformBoldRadius Solutions
 
Banner locations of Pragyan
Banner locations of PragyanBanner locations of Pragyan
Banner locations of PragyanVishnu Gopal
 
Corner Jetted Bathtubs
Corner Jetted BathtubsCorner Jetted Bathtubs
Corner Jetted BathtubsAquaticabath
 
6 benso, alban. - da micro-historia
6   benso, alban. - da micro-historia6   benso, alban. - da micro-historia
6 benso, alban. - da micro-historiaMilaysa Paz
 
Week #7 looking for killer b's
Week #7  looking for killer b'sWeek #7  looking for killer b's
Week #7 looking for killer b'sMathew Olasupo
 

Viewers also liked (16)

Case class scala
Case class scalaCase class scala
Case class scala
 
Pattern Matching in Scala
Pattern Matching in ScalaPattern Matching in Scala
Pattern Matching in Scala
 
Intro to pattern matching in scala
Intro to pattern matching in scalaIntro to pattern matching in scala
Intro to pattern matching in scala
 
Run-Away-Bear (1984)
Run-Away-Bear (1984)Run-Away-Bear (1984)
Run-Away-Bear (1984)
 
Finishing brands atomization technology - vietnamese
Finishing brands   atomization technology - vietnameseFinishing brands   atomization technology - vietnamese
Finishing brands atomization technology - vietnamese
 
A Good Designed Website Relieves Your Business
A Good Designed Website Relieves Your BusinessA Good Designed Website Relieves Your Business
A Good Designed Website Relieves Your Business
 
James D. Beers CV (5-2-16)
James D. Beers CV (5-2-16)James D. Beers CV (5-2-16)
James D. Beers CV (5-2-16)
 
Ferdigheter for en nav veileder
Ferdigheter for en nav  veilederFerdigheter for en nav  veileder
Ferdigheter for en nav veileder
 
One stop english
One stop englishOne stop english
One stop english
 
Corporate intro presentation
Corporate intro presentationCorporate intro presentation
Corporate intro presentation
 
Introduction to the Typesafe Reactive Platform
Introduction to the Typesafe Reactive PlatformIntroduction to the Typesafe Reactive Platform
Introduction to the Typesafe Reactive Platform
 
Banner locations of Pragyan
Banner locations of PragyanBanner locations of Pragyan
Banner locations of Pragyan
 
Corner Jetted Bathtubs
Corner Jetted BathtubsCorner Jetted Bathtubs
Corner Jetted Bathtubs
 
6 benso, alban. - da micro-historia
6   benso, alban. - da micro-historia6   benso, alban. - da micro-historia
6 benso, alban. - da micro-historia
 
Elektriskie lādiņi
Elektriskie lādiņiElektriskie lādiņi
Elektriskie lādiņi
 
Week #7 looking for killer b's
Week #7  looking for killer b'sWeek #7  looking for killer b's
Week #7 looking for killer b's
 

Similar to Demonstrating Case Classes in Scala

Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritancemcollison
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part IIIHari Christian
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingShivam Singhal
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHPRamasubbu .P
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelRamrao Desai
 
Advanced php
Advanced phpAdvanced php
Advanced phphamfu
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxDavidLazar17
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in javaAtul Sehdev
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHPwahidullah mudaser
 
14 Defining classes
14 Defining classes14 Defining classes
14 Defining classesmaznabili
 

Similar to Demonstrating Case Classes in Scala (20)

Scala: Collections API
Scala: Collections APIScala: Collections API
Scala: Collections API
 
Lecture Notes
Lecture NotesLecture Notes
Lecture Notes
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
03 Java Language And OOP Part III
03 Java Language And OOP Part III03 Java Language And OOP Part III
03 Java Language And OOP Part III
 
Learn Java Part 10
Learn Java Part 10Learn Java Part 10
Learn Java Part 10
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 
10 classes
10 classes10 classes
10 classes
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Java session4
Java session4Java session4
Java session4
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
14 Defining classes
14 Defining classes14 Defining classes
14 Defining classes
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 

More from BoldRadius Solutions

Towards Reliable Lookups - Scala By The Bay
Towards Reliable Lookups - Scala By The BayTowards Reliable Lookups - Scala By The Bay
Towards Reliable Lookups - Scala By The BayBoldRadius Solutions
 
How You Convince Your Manager To Adopt Scala.js in Production
How You Convince Your Manager To Adopt Scala.js in ProductionHow You Convince Your Manager To Adopt Scala.js in Production
How You Convince Your Manager To Adopt Scala.js in ProductionBoldRadius Solutions
 
Why Not Make the Transition from Java to Scala?
Why Not Make the Transition from Java to Scala?Why Not Make the Transition from Java to Scala?
Why Not Make the Transition from Java to Scala?BoldRadius Solutions
 
String Interpolation in Scala | BoldRadius
String Interpolation in Scala | BoldRadiusString Interpolation in Scala | BoldRadius
String Interpolation in Scala | BoldRadiusBoldRadius Solutions
 
Value Classes in Scala | BoldRadius
Value Classes in Scala | BoldRadiusValue Classes in Scala | BoldRadius
Value Classes in Scala | BoldRadiusBoldRadius Solutions
 
Scala Days Highlights | BoldRadius
Scala Days Highlights | BoldRadiusScala Days Highlights | BoldRadius
Scala Days Highlights | BoldRadiusBoldRadius Solutions
 
What Are For Expressions in Scala?
What Are For Expressions in Scala?What Are For Expressions in Scala?
What Are For Expressions in Scala?BoldRadius Solutions
 
Domain Driven Design Through Onion Architecture
Domain Driven Design Through Onion ArchitectureDomain Driven Design Through Onion Architecture
Domain Driven Design Through Onion ArchitectureBoldRadius Solutions
 
How To Use Higher Order Functions in Scala
How To Use Higher Order Functions in ScalaHow To Use Higher Order Functions in Scala
How To Use Higher Order Functions in ScalaBoldRadius Solutions
 
Scala Days 2014: Pitching Typesafe
Scala Days 2014: Pitching TypesafeScala Days 2014: Pitching Typesafe
Scala Days 2014: Pitching TypesafeBoldRadius Solutions
 
Functional Programming - Worth the Effort
Functional Programming - Worth the EffortFunctional Programming - Worth the Effort
Functional Programming - Worth the EffortBoldRadius Solutions
 

More from BoldRadius Solutions (17)

Partial Functions in Scala
Partial Functions in ScalaPartial Functions in Scala
Partial Functions in Scala
 
Towards Reliable Lookups - Scala By The Bay
Towards Reliable Lookups - Scala By The BayTowards Reliable Lookups - Scala By The Bay
Towards Reliable Lookups - Scala By The Bay
 
How You Convince Your Manager To Adopt Scala.js in Production
How You Convince Your Manager To Adopt Scala.js in ProductionHow You Convince Your Manager To Adopt Scala.js in Production
How You Convince Your Manager To Adopt Scala.js in Production
 
Introduction to the Actor Model
Introduction to the Actor ModelIntroduction to the Actor Model
Introduction to the Actor Model
 
Why Not Make the Transition from Java to Scala?
Why Not Make the Transition from Java to Scala?Why Not Make the Transition from Java to Scala?
Why Not Make the Transition from Java to Scala?
 
String Interpolation in Scala | BoldRadius
String Interpolation in Scala | BoldRadiusString Interpolation in Scala | BoldRadius
String Interpolation in Scala | BoldRadius
 
Value Classes in Scala | BoldRadius
Value Classes in Scala | BoldRadiusValue Classes in Scala | BoldRadius
Value Classes in Scala | BoldRadius
 
Scala Days Highlights | BoldRadius
Scala Days Highlights | BoldRadiusScala Days Highlights | BoldRadius
Scala Days Highlights | BoldRadius
 
What Are For Expressions in Scala?
What Are For Expressions in Scala?What Are For Expressions in Scala?
What Are For Expressions in Scala?
 
Domain Driven Design Through Onion Architecture
Domain Driven Design Through Onion ArchitectureDomain Driven Design Through Onion Architecture
Domain Driven Design Through Onion Architecture
 
Pattern Matching in Scala
Pattern Matching in ScalaPattern Matching in Scala
Pattern Matching in Scala
 
What are Sealed Classes in Scala?
What are Sealed Classes in Scala?What are Sealed Classes in Scala?
What are Sealed Classes in Scala?
 
How To Use Higher Order Functions in Scala
How To Use Higher Order Functions in ScalaHow To Use Higher Order Functions in Scala
How To Use Higher Order Functions in Scala
 
Immutability in Scala
Immutability in ScalaImmutability in Scala
Immutability in Scala
 
Scala Days 2014: Pitching Typesafe
Scala Days 2014: Pitching TypesafeScala Days 2014: Pitching Typesafe
Scala Days 2014: Pitching Typesafe
 
Code Brevity in Scala
Code Brevity in ScalaCode Brevity in Scala
Code Brevity in Scala
 
Functional Programming - Worth the Effort
Functional Programming - Worth the EffortFunctional Programming - Worth the Effort
Functional Programming - Worth the Effort
 

Recently uploaded

Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 

Recently uploaded (20)

Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 

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