[英]How to declare and pass arguments to implicit parameters in Scala 3?
I have this code in scala 2我在 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
}
}
Here x
parameter of count
function is annotated as implicit
so it is able to be passed into the double
function implicitly.这里count
function 的x
参数被注释为implicit
,因此它能够隐式传递到double
function 中。 How can I do this in Scala 3
using given-using / summon?如何使用给定使用/召唤在Scala 3
中执行此操作?
The relevant docs section is Relationship with Scala 2 Implicits - using clauses which explains相关文档部分是与 Scala 2 Implicits 的关系 - 使用解释的条款
Explicit arguments to parameters of using clauses must be written using
(using...)
, mirroring the definition syntax.显式 arguments 到 using 子句的参数必须使用(using...)
编写,镜像定义语法。
So definitions所以定义
def double(using y: Int) = y*2
def count(using x: Int) = double
can be applied like so可以这样应用
count(using number)
Note how conceptually the same keyword using
is meant to convey both the idea of "requirement" at the definition-site and the idea of "provision" at the call-site.请注意,在概念上, 相同的关键字using
是如何同时传达定义站点的“需求”概念和调用站点的“供应”概念。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.