Created
June 18, 2013 17:18
-
-
Save leifwickland/5807398 to your computer and use it in GitHub Desktop.
In Scala, require a type parameter has multiple typeclasses.
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
class A { | |
def f[T:CanQ:CanR](t: T) { | |
val q = implicitly[CanQ[T]] | |
println(q.q(t)) | |
val r = implicitly[CanR[T]] | |
println(r.r(t)) | |
} | |
} | |
trait CanQ[T] { | |
def q(t: T): String | |
} | |
trait CanR[T] { | |
def r(t: T): String | |
} | |
object Main { | |
implicit object Q extends CanQ[String] { | |
def q(t: String): String = "Q: " + t.toString | |
} | |
implicit object R extends CanR[String] { | |
def r(t: String): String = "R: " + t.toString | |
} | |
def main(a: Array[String]) { | |
new A().f("some string") | |
} | |
} |
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
Q: some string | |
R: some string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment