Create a gist now

Instantly share code, notes, and snippets.

@samdvr /adt.scala Secret
Last active Aug 23, 2017

What would you like to do?
Algebraic Data Types
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