I have this code in 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. How can I do this in Scala 3
using given-using / summon?
The relevant docs section is Relationship with Scala 2 Implicits - using clauses which explains
Explicit arguments to parameters of using clauses must be written using
(using ...)
, mirroring the definition syntax.
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.