Skip to content

Instantly share code, notes, and snippets.

@ocadaruma
Last active July 28, 2017 16:57
Show Gist options
  • Save ocadaruma/fd5645f13f361d99a3b58eac2fdcbd60 to your computer and use it in GitHub Desktop.
Save ocadaruma/fd5645f13f361d99a3b58eac2fdcbd60 to your computer and use it in GitHub Desktop.
def doSomething(strategy: String => Unit): Unit = {
println("start")
// call strategy
strategy("SOME STRING")
println("end")
}
def justPrint(s: String): Unit = println(s)
doSomething(justPrint) // => "SOME STRING" が表示される
// String => Unit は Function1[String, Unit]のことで、Function1の第一型パラメータは反変.
// つまり、Any => Unitも渡せる
def printWithPrefix(a: Any): Unit = println("prefix_" + a.toString)
doSomething(printWithPrefix) // => "prefix_SOME STRING" が表示される
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment