0

我在scala 2中有这个代码

val number = 20

def double(implicit y:Int)={
  y*2
}

def count(implicit x:Int)={
  double
}

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println(count(number)) // res: 40
  }
}

这里 x对函数的参数进行了count注释,implicit因此它可以double隐式传递给函数。Scala 3在使用给定使用/召唤时如何做到这一点?

4

1 回答 1

4

相关文档部分是与 Scala 2 Implicits 的关系 - 使用子句解释

using 子句的参数的显式参数必须使用 using 编写(using ...),镜像定义语法。

所以定义

def double(using y: Int) = y*2
def count(using x: Int) = double

可以这样应用

count(using number)

请注意,从概念上讲,相同的关键字using是如何同时传达定义站点的“需求”概念和调用站点的“供应”概念的。

于 2021-03-02T14:35:43.580 回答