Last active
November 9, 2017 04:27
-
-
Save guangningyu/f6c9d52b12888bd892d5aac45287e904 to your computer and use it in GitHub Desktop.
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
object HelloWorld { | |
case class Text(content: String) | |
case class Prefix(text: String) | |
// step 2: capture the String conversion in implicit function; | |
// then find an implicit parameter and end up with finding "prefixLOL" | |
implicit def String2Text(content: String)(implicit prefix: Prefix) = { | |
// step 3: return instance of Text | |
Text(prefix.text + " " + content) | |
} | |
// step 1: need to convert "String" to "Text" | |
def printText(text: Text): Unit = { | |
// step 4: print content | |
println(text.content) | |
} | |
def main(args: Array[String]): Unit = { | |
printText("World!") | |
} | |
// step 0: initialize an implicit instance of class Prefix | |
implicit val prefixLOL = Prefix("Hello") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment