我找不到以下语法规则的解释:
FunType ::= FunTypeArgs (‘=>’ | ‘?=>’) Type
?=>
表示一个上下文函数类型。
Context functions are written using
?=>
as the “arrow” sign. They are applied to synthesized arguments, in the same way methods with context parameters are applied. For instance:given ec: ExecutionContext = ... def f(x: Int): ExecutionContext ?=> Int = ...
...
f(2)(using ec) // explicit argument f(2) // argument is inferred
因此,如果您将A => B
视为类比于
def foo(a: A): B
A ?=> B
理解为类似于:def foo(using a: A): B
这就像是一个普通的函数,只不过参数被作为上下文参数传递。您可以拒绝提供它(并且它将从所有给定的范围内推断出来,类似于Scala 2中的implicit
),或者您可以使用using
关键字明确提供它。