Skip to content

Instantly share code, notes, and snippets.

@abhandaru
Created March 18, 2020 09:22
Show Gist options
  • Save abhandaru/915771ed2e074c4e67c04d400f64f5b5 to your computer and use it in GitHub Desktop.
Save abhandaru/915771ed2e074c4e67c04d400f64f5b5 to your computer and use it in GitHub Desktop.
trait Foo { def i: Int }
trait Foo2[T] { def sq(t: T): Int }
trait Bar[T] {
def cube(t: T): Int
}
object Bar {
implicit def foo[T <: Foo: Foo2]: Bar[T] = {
val ctx = implicitly[Foo2[T]]
new Bar[T] { def cube(t: T) = t.i * ctx.sq(t) }
}
}
case class Adu(i: Int) extends Foo
def use[T <: Foo: Foo2](t: T) = true
def use2[T: Bar](t: T) = true
implicit val aduIsFoo2 = new Foo2[Adu] { def sq(t: Adu) = t.i * t.i }
use(Adu(2)) // OK!
use2(Adu(4)) // OK!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment