2016-11-05 1 views
6

global 실행 컨텍스트를 가져온 다음 범위에 정의 된 다른 암시 적 실행 컨텍스트를 사용하면 모호한 암시 적 결과가 발생하지만 2.11에서는 올바르게 작동합니다.Scala 2.12의 암시 적 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] 
         ^

어떤이의 원인은 무엇이며 어떻게 코드에서 해결하기 :

import scala.concurrent._ 
import scala.concurrent.ExecutionContext.Implicits.global 

class A(implicit ec: ExecutionContext) { 
    x = implicitly[ExecutionContext] 
} 

컴파일러 오류를 준다?

+1

스칼라/스칼라 저장소의 관련 커밋은 https://github.com/scala/scala/commit/44953dcb08fc5dd92e423a56bd42bcc32757aaef이며 변경에 대한 정당성은 https://issues.scala-lang.org/browse/SI-8849입니다. –

+0

빅토르 클랑 (Viktor Klang)은 JIRA 티켓에 대해 "ExecutionContext.global을 명시 적으로 전달하거나 필요할 때이를 가리키는 암시 적 val을 도입 할 것을 권장합니다" –

+0

2.11에서이 문제를 올바르게 이해하면 2.11에서 실제로 "잘못"또는 매우 범위에서 내재적으로 "전역"이 선택되면서 예기치 않게? 그리고 2.12에서는이 잠재적 인 프로그래밍 실수를 모호하게 만들어 설명합니다. 릴리스 노트 및/또는 설명서에서 언급 할만한 가치가 있습니다. – ochrons

답변

2

스펙은 클래스 구성원 선택의 모호성으로 과부하 해결을 처리합니다. 하지만 암시 적 해상도는 정적 오버로드 해상도를 사용하여 구성원이 아닌 참조를 선택합니다. zzzyyy만큼 X에서 파생 된 클래스에 정의되어 있기 때문에

논란의 여지가, 다음은, 사양의 잘못된 해석입니다 : 현재

$ scala 
Welcome to Scala 2.12.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101). 
Type in expressions for evaluation. Or try :help. 

scala> import concurrent._, ExecutionContext.global 
import concurrent._ 
import ExecutionContext.global 

scala> trait X { implicit val xxx: ExecutionContext = global } 
defined trait X 

scala> class Y extends X { implicit val yyy: ExecutionContext = global ; def f = implicitly[ExecutionContext] } 
defined class Y 

scala> class Z extends X { def f(implicit zzz: ExecutionContext) = implicitly[ExecutionContext] } 
<console>:16: error: ambiguous implicit values: 
both value xxx in trait X of type => scala.concurrent.ExecutionContext 
and value zzz of type scala.concurrent.ExecutionContext 
match expected type scala.concurrent.ExecutionContext 
     class Z extends X { def f(implicit zzz: ExecutionContext) = implicitly[ExecutionContext] } 
                      ^

, 당신은 둘러싸에서 암시 그림자에 이름에 의존해야 범위 :

scala> class Z extends X { def f(implicit xxx: ExecutionContext) = implicitly[ExecutionContext] } 
defined class Z 

또는

scala> :pa 
// Entering paste mode (ctrl-D to finish) 

package object p { import concurrent._ ; implicit val xxx: ExecutionContext = ExecutionContext.global } 
package p { import concurrent._ ; 
    class P { def f(implicit xxx: ExecutionContext) = implicitly[ExecutionContext] 
      def g = implicitly[ExecutionContext] } 
} 

// Exiting paste mode, now interpreting. 


scala> 
관련 문제