Created
September 17, 2012 15:43
-
-
Save bkyrlach/3738096 to your computer and use it in GitHub Desktop.
Duck typing example..
This file contains hidden or 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 Program { | |
| def run(): Unit = println("I pass a Turing test.") | |
| } | |
| class Person { | |
| def run(): Unit = println("But I don't.") | |
| } | |
| class Engine { | |
| def run(): String = "I run in a totally different way" | |
| } | |
| object DuckTyping { | |
| def makeRun(runner: { def run(): Unit }): Unit = runner.run | |
| val a = new Program | |
| val b = new Person | |
| val c = new Engine | |
| val d = new Object | |
| makeRun(a) | |
| makeRun(b) | |
| //makeRun(c) //Won't compile | |
| //makeRun(d) //Won't compile | |
| } |
This file contains hidden or 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 Program { | |
| def run(): Unit = println("I pass a Turing test.") | |
| } | |
| class Person { | |
| def run(): Unit = println("But I don't.") | |
| } | |
| class Engine { | |
| def run(): String = "I run in a totally different way" | |
| } | |
| object DuckTyping { | |
| type CanRun = { | |
| def run(): Unit | |
| } | |
| def makeRun(runner: CanRun): Unit = runner.run | |
| val a = new Program | |
| val b = new Person | |
| val c = new Engine | |
| val d = new Object | |
| makeRun(a) | |
| makeRun(b) | |
| //makeRun(c) //Won't compile | |
| //makeRun(d) //Won't compile | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment