Skip to content

Instantly share code, notes, and snippets.

@bkyrlach
Created September 17, 2012 15:43
Show Gist options
  • Select an option

  • Save bkyrlach/3738096 to your computer and use it in GitHub Desktop.

Select an option

Save bkyrlach/3738096 to your computer and use it in GitHub Desktop.
Duck typing example..
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
}
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