Skip to content

Instantly share code, notes, and snippets.

@tbje
Created June 25, 2015 16:09
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 tbje/ec9d45b6559bf405c50e to your computer and use it in GitHub Desktop.
Save tbje/ec9d45b6559bf405c50e to your computer and use it in GitHub Desktop.
Feeding animals
package misc
// Animal
abstract class Animal {
def name: String
def eat(food: Food): Animal =
this
}
case class Bird(name: String) extends Animal
case class Cow(name: String) extends Animal
// Food
abstract class Food
case object Grains extends Food
case object Grass extends Food
// Feeding
object FeedingAnimals {
def main(args: Array[String]): Unit = {
val bill = Bird("Bill")
bill eat Grains
bill eat Grass // Shouldn't compile
val cindy = Cow("Cindy")
cindy eat Grass
cindy eat Grains // Shouldn't compile
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment