Created
June 15, 2011 23:14
-
-
Save kings13y/1028362 to your computer and use it in GitHub Desktop.
Static Duck Typing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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