Created
June 25, 2015 16:09
-
-
Save tbje/ec9d45b6559bf405c50e to your computer and use it in GitHub Desktop.
Feeding animals
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
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