Skip to content

Instantly share code, notes, and snippets.

@guangningyu
Last active November 9, 2017 04:27
Show Gist options
  • Save guangningyu/f6c9d52b12888bd892d5aac45287e904 to your computer and use it in GitHub Desktop.
Save guangningyu/f6c9d52b12888bd892d5aac45287e904 to your computer and use it in GitHub Desktop.
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