Skip to content

Instantly share code, notes, and snippets.

@robinst
Created May 12, 2011 12:51
Show Gist options
  • Save robinst/968432 to your computer and use it in GitHub Desktop.
Save robinst/968432 to your computer and use it in GitHub Desktop.
Duck Typing in Scala
class Duck {
def quack = println("The duck quacks")
def walk = println("The duck walks")
}
class Dog {
def quack = println("The dog quacks (barks)")
def walk = println("The dog walks")
}
def testDuckTyping(animal: { def quack; def walk }) = {
animal.quack
animal.walk
}
scala> testDuckTyping(new Duck)
The duck quacks
The duck walks
scala> testDuckTyping(new Dog)
The dog quacks (barks)
The dog walks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment