I have a Scala code similar to the code below. There is an implicit defined at the class / object level and I want to 'override' it with an implicit defined in the method. (If you really need to know, I need to change my ExecutionContext
specially for the method in question).
In this example, I want to use b
as the implicit for Int
in the scope of the bar
method. How can I do this?
object Foo {
implicit val a: Int = 1
def bar: Int = { // Didn't pass implicit Int parameter
implicit val b: Int = 2
implicitly[Int]
}
}
This complains with
error: ambiguous implicit values: both value a in object Foo of type Int and value b of type Int match expected type Int
So I think Scala cannot choose between the class-scope implicit and method-scope implicit.