Skip to content

Instantly share code, notes, and snippets.

@ebruchez
Last active August 29, 2015 14:10
Show Gist options
  • Save ebruchez/e62dd70f2727d4d700c7 to your computer and use it in GitHub Desktop.
Save ebruchez/e62dd70f2727d4d700c7 to your computer and use it in GitHub Desktop.
trait Animal { def name: String; def word: String; def talk() = println(s"$name says $word") }
trait Flying { def fly() = println("flap flap") }
trait Swimming { def swim() = println("splish splash") }
trait Walking { def walk() = println("stomp stomp") }
def newAlligator =
new Animal with Walking with Swimming { val name = "alligator"; val word = "grrr" }
def newDuck =
new Animal with Walking with Flying with Swimming { val name = "duck"; val word = "quack" }
def newGoat =
new Animal with Walking { val name = "goat"; val word = "baa" }
val alligator = newAlligator
val duck = newDuck
val goat = newGoat
scala> alligator.talk()
alligator says grrr
scala> alligator.walk()
stomp stomp
scala> alligator.swim()
splish splash
scala> duck.talk()
duck says quack
scala> duck.walk()
stomp stomp
scala> duck.fly()
flap flap
scala> duck.swim()
splish splash
scala> goat.walk()
stomp stomp
scala> goat.talk()
goat says baa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment