Created
March 18, 2020 09:22
-
-
Save abhandaru/915771ed2e074c4e67c04d400f64f5b5 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
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