Last active
July 28, 2017 16:57
-
-
Save ocadaruma/fd5645f13f361d99a3b58eac2fdcbd60 to your computer and use it in GitHub Desktop.
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
| 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