Skip to content

Instantly share code, notes, and snippets.

@leifwickland
Created June 18, 2013 17:18
Show Gist options
  • Save leifwickland/5807398 to your computer and use it in GitHub Desktop.
Save leifwickland/5807398 to your computer and use it in GitHub Desktop.
In Scala, require a type parameter has multiple typeclasses.
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")
}
}
Q: some string
R: some string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment