Scala 3中"?=>"的含义是什么?

13

我找不到以下语法规则的解释:

FunType ::= FunTypeArgs (‘=>’ | ‘?=>’) Type
1个回答

15

?=> 表示一个上下文函数类型。

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关键字明确提供它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接