Last active
September 21, 2018 03:44
-
-
Save seldridge/43f7b9113df3b232b2d3200db1f69cca 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
/* | |
The default error message is: | |
/home/se/tmp/implicit.scala:41: error: could not find implicit value for parameter c: this.Converter[String] | |
println(view[String](1)) | |
^ | |
We could change this to something better..., e.g., | |
/home/se/tmp/implicit.scala:41: error: No implicit `Converter` for type String found in scope. Did you forget to import it? | |
println(view[String](1)) | |
^ | |
*/ | |
import scala.annotation.implicitNotFound | |
@implicitNotFound("No implicit `Converter` for type ${T} found in scope. Did you forget to import it?") | |
trait Converter[T] { | |
def convert(x: Int): T | |
} | |
def view[T](x: Int)(implicit c: Converter[T]): T = c.convert(x) | |
object IntConverter { | |
implicit object IntView extends Converter[Int] { | |
def convert(x: Int) = x | |
} | |
} | |
object StringConverter { | |
implicit object StringView extends Converter[String] { | |
def convert(x: Int) = "hello" | |
} | |
} | |
import IntConverter._ | |
// import StringConverter._ | |
println(view[Int](1)) | |
println(view[String](1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment