Created
March 20, 2018 16:23
-
-
Save stevebakh/7f39fd752780ef7a12a724da2fdc98f5 to your computer and use it in GitHub Desktop.
Just playing around with context bounds and implicit type classes. Shows the two forms that can be used to access the implicit typeclass.
This file contains hidden or 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 A { | |
| val name = "Aaaaay" | |
| } | |
| class B extends A | |
| trait C[T <: A] { | |
| def gofuckyourself(t: T): String | |
| } | |
| implicit val typeclass: C[B] = new C[B] { | |
| override def gofuckyourself(t: B) = t.name + ", go fuck yourself!" | |
| } | |
| def foo[T <: A : C](input: T): Unit = { | |
| println(implicitly[C[T]].gofuckyourself(input)) | |
| } | |
| def bar[T <: A](input: T)(implicit ev: C[T]): Unit = { | |
| println(ev.gofuckyourself(input)) | |
| } | |
| foo(new B()) | |
| bar(new B()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment