Skip to content

Instantly share code, notes, and snippets.

@tkawachi
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkawachi/8e4987441f775786d1d8 to your computer and use it in GitHub Desktop.
Save tkawachi/8e4987441f775786d1d8 to your computer and use it in GitHub Desktop.
case class A(s: String)
object A {
implicit val defaultA = A("defaultA")
}
case class B(a: A)
trait T {
implicit def a(implicit b: B): A = b.a
}
class C extends T {
def y()(implicit a: A): Unit = {
println("implicit A in y:" + a)
}
def f1(implicit b: B): Unit = {
// prints "expectedA"
println("implicit A in f1:" + implicitly[A])
// prints "expectedA"
y()
}
/*
def f2(implicit b: B): Unit = {
// prints "expectedA" in 2.10
println("implicit A in f2:" + implicitly[A])
// compile error in 2.11, compile in 2.10
implicit val ia = implicitly[A]
// prints "defaultA" in 2.10, maybe an old bug?
y()
}
*/
def f3(implicit b: B): Unit = {
// prints "defaultA", why?
println("implicit A in f3:" + implicitly[A])
val a = implicitly[A]
// prints "defaultA", why?
y()
}
def f4(implicit b: B): Unit = {
// prints "expectedA"
y()(implicitly[A])
}
def f5(implicit b: B): Unit = {
// prints "expectedA"
val x = implicitly[A]
y()(x)
}
def f6(implicit b: B): Unit = {
// prints "defaultA" ??? >_<
val a = implicitly[A]
y()(a)
}
def f7(implicit b: B): Unit = {
val a = "abc"
// prints "defaultA".
// Isn't it a bug of scala compiler?
println(implicitly[A])
}
def f8(a: String)(implicit b: B): Unit = {
// It's shadowed by an argument, too
// prints "defaultA".
println(implicitly[A])
}
}
object Main {
def main(args: Array[String]): Unit = {
println("Hello")
implicit val b = B(A("expectedA"))
val c = new C()
c.f1
// c.f2
c.f3
c.f4
c.f5
c.f6
c.f7
c.f8("abc")
}
}
@tkawachi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment