Skip to content

Instantly share code, notes, and snippets.

@vvviiimmm
Created February 18, 2017 19:31
Show Gist options
  • Select an option

  • Save vvviiimmm/d0ad79af8d1a5a96a08aa0b5c1f46830 to your computer and use it in GitHub Desktop.

Select an option

Save vvviiimmm/d0ad79af8d1a5a96a08aa0b5c1f46830 to your computer and use it in GitHub Desktop.
// Our generic interface
trait Animal {
def voice: String
}
// Pure definitions
case class Dog()
case class Cat()
// Implementation 1
class DogAnimal extends Animal {
override def voice: String = "woof!"
}
// Implementation 2
class CatAnimal extends Animal {
override def voice: String = "meow!"
}
// Generic function
def say(animal: Animal) = println(animal.voice)
// Usage
say(new DogAnimal) // prints "woof!"
say(new CatAnimal) // prints "meow!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment