Implicit ExecutionContext priority in Scala 2.12 -
in scala 2.12 importing global
execution context , having implicit execution context defined in scope results in ambiguous implicit, while in 2.11 works fine.
import scala.concurrent._ import scala.concurrent.executioncontext.implicits.global class a(implicit ec: executioncontext) { x = implicitly[executioncontext] }
compiler gives error:
error: ambiguous implicit values: both lazy value global in object implicits of type => scala.concurrent.executioncontext , value ec in class of type scala.concurrent.executioncontext match expected type scala.concurrent.executioncontext val x = implicitly[executioncontext] ^
what cause of , how work around in code?
the spec treats overload resolution disambiguation of selection of members of class. implicit resolution uses static overload resolution choose between references not members.
arguably, following misinterpretation of spec, since zzz
defined in class derived x
yyy
is:
$ scala welcome scala 2.12.0 (java hotspot(tm) 64-bit server vm, java 1.8.0_101). type in expressions 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 , value zzz of type scala.concurrent.executioncontext match expected type scala.concurrent.executioncontext class z extends x { def f(implicit zzz: executioncontext) = implicitly[executioncontext] } ^
currently, must rely on naming shadow implicit enclosing scope:
scala> class z extends x { def f(implicit xxx: executioncontext) = implicitly[executioncontext] } defined class z
or,
scala> :pa // entering paste mode (ctrl-d 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, interpreting. scala>
Comments
Post a Comment