Skip to content

Instantly share code, notes, and snippets.

@shunjikonishi
Created May 13, 2015 10:58
Show Gist options
  • Save shunjikonishi/967d91b4acd60866ae81 to your computer and use it in GitHub Desktop.
Save shunjikonishi/967d91b4acd60866ae81 to your computer and use it in GitHub Desktop.
Simple example of implicit def.(REPL)
scala> val a = "Jack"
a: String = Jack
scala> a.hello
<console>:9: error: value hello is not a member of String
a.hello
^
scala> class B(s: String) {
| def hello = "Hello " + s
| }
defined class B
scala> val b = new B("Queen")
b: B = B@2d209079
scala> b.hello
res1: String = Hello Queen
scala> implicit def str2B(s: String) = new B(s)
warning: there was one feature warning; re-run with -feature for details
str2B: (s: String)B
scala> a.hello
res2: String = Hello Jack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment