-
-
Save samdvr/56465f19c24876bec2ba793be92df78d to your computer and use it in GitHub Desktop.
Algebraic Data Types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait Food { | |
val calories:Double | |
} | |
case class Pizza(calories: Double, crust: String) extends Food | |
case class Hamburger(calories: Double, cheese:Boolean) extends Food | |
sealed trait Person { | |
val name:String | |
} | |
case class Student(name: String) extends Person | |
case class Programmer(name: String) extends Person | |
def printDetails(person: Person, food: Food) = { | |
print(s" Hello ${person.name}, your food has ${food.calories} calories.") | |
} | |
printDetails(Student("Rob"), Pizza(1000, "thin")) // Hello Rob, your food has 1000 calories. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment