在Scala 2.12中导入global执行上下文然后在作用域中定义了另一个隐式执行上下文导致模糊隐式,而在2.11中它可以正常工作.
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
class A(implicit ec: ExecutionContext) {
x = implicitly[ExecutionContext]
}
Run Code Online (Sandbox Code Playgroud)
编译器给出错误:
error: ambiguous implicit values:
both lazy value global in object Implicits of type => scala.concurrent.ExecutionContext
and value ec in class A of type scala.concurrent.ExecutionContext
match expected type scala.concurrent.ExecutionContext
val x = implicitly[ExecutionContext]
^
Run Code Online (Sandbox Code Playgroud)
这是什么原因以及如何在代码中解决它?