如何声明参数并将参数传递给 Scala 3 中的隐式参数?
How to declare and pass arguments to implicit parameters in Scala 3?
我在 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
}
}
此处 count
函数的 x
参数被注释为 implicit
因此它可以隐式传递给 double
函数。我如何在 Scala 3
中使用 given-using / summon 来做到这一点?
相关文档部分是 Relationship with Scala 2 Implicits - using clauses,它解释了
Explicit arguments to parameters of using clauses must be written
using (using ...)
, mirroring the definition syntax.
所以定义
def double(using y: Int) = y*2
def count(using x: Int) = double
可以这样应用
count(using number)
请注意 same 关键字 using
在概念上是如何传达定义站点的“要求”和调用站点的“规定”的概念。
我在 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
}
}
此处 count
函数的 x
参数被注释为 implicit
因此它可以隐式传递给 double
函数。我如何在 Scala 3
中使用 given-using / summon 来做到这一点?
相关文档部分是 Relationship with Scala 2 Implicits - using clauses,它解释了
Explicit arguments to parameters of using clauses must be written using
(using ...)
, mirroring the definition syntax.
所以定义
def double(using y: Int) = y*2
def count(using x: Int) = double
可以这样应用
count(using number)
请注意 same 关键字 using
在概念上是如何传达定义站点的“要求”和调用站点的“规定”的概念。