Created
May 12, 2011 12:51
-
-
Save robinst/968432 to your computer and use it in GitHub Desktop.
Duck Typing in Scala
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 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