小编och*_*ons的帖子

Scala 2.12中的隐式ExecutionContext优先级

在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)

这是什么原因以及如何在代码中解决它?

scala implicit scala-2.12

7
推荐指数
1
解决办法
1546
查看次数

标签 统计

implicit ×1

scala ×1

scala-2.12 ×1