Skip to content

Instantly share code, notes, and snippets.

@retronym
Created December 16, 2009 10:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save retronym/257748 to your computer and use it in GitHub Desktop.
Save retronym/257748 to your computer and use it in GitHub Desktop.
How to Chain Implicit Conversions (aka Views) in Scala
class T {
val t = "T"
}
class U
class V
object T {
implicit def UToT[UU <% U](u: UU) = new T
}
object U {
implicit def VToU[VV <% V](v: VV) = new U
}
object V {
implicit def StringToV(s: String) = new V
}
object Test {
import T._
import U._
import V._
"": T
// compiles to:
// (T.UToT[java.lang.String]("")({
// ((v: java.lang.String) => U.VToU[java.lang.String](v)({
// ((s: String) => V.StringToV(s))
// }))
// }): T)
"".t // this also triggers the conversion.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment