在Scala 2.12中,导入global
执行上下文并在作用域中定义另一个隐式执行上下文将导致模棱两可的隐式情况,而在2.11中则完全正常。
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
class A(implicit ec: ExecutionContext) {
x = implicitly[ExecutionContext]
}
编译器出现错误:
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]
^
这是什么原因,如何在代码中解决?