Created
May 13, 2015 10:58
-
-
Save shunjikonishi/967d91b4acd60866ae81 to your computer and use it in GitHub Desktop.
Simple example of implicit def.(REPL)
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
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