Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Static Duck Typing
class Duck {
def squawk = println("Quack")
def waddle = println("Duck walk")
class Pengiun {
def squawk = println("Squeek")
def waddle = println("Penguin walk")
class Person { }
// everybody's heard about the word...
def birdIsTheWord(bird : { def squawk; def waddle}) = {
birdIsTheWord(new Duck()) // prints "Quack" then "Duck walk"
birdIsTheWord(new Penguin()) // prints "Squeek" then "Penguin walk"
birdIsTheWord(new Person()) // Will not compile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.