Consider the following code:
class A { def print = println("A") }
class B extends A { override def print = println("B") }
def foo(implicit a: A) = a.print
def bar(implicit a: A) = {
implicit val b = new B
foo
}
bar(new A) // B
I am wondering why calling foo
in bar
isn't raising an ambiguous implicit values
error. Of course
implicit val b: A = new B
will raise that error. Why does foo
pick the implicit b
and not the implicit a
? Or even more general: What are the rules what will be picked?
EDIT:
Due to my comment-conversation with Ivan I want to clarify: I would know the answer to my question if I named the local implicit val the same way as the implicit method parameter.
def bar(implicit a: A) = {
implicit val a = new B
foo
}
Then only the local val a
is in scope which scope-overrides the method parameter because they have the same name.