Skip to content

Instantly share code, notes, and snippets.

@kings13y
Created June 15, 2011 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kings13y/1028362 to your computer and use it in GitHub Desktop.
Save kings13y/1028362 to your computer and use it in GitHub Desktop.
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}) = {
bird.squawk
bird.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