Skip to content

Instantly share code, notes, and snippets.

@samdvr

samdvr/adt.scala Secret

Last active August 23, 2017 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samdvr/56465f19c24876bec2ba793be92df78d to your computer and use it in GitHub Desktop.
Save samdvr/56465f19c24876bec2ba793be92df78d to your computer and use it in GitHub Desktop.
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