given and using
A given is a value the compiler can supply at a call site where the code asked for it, and a using parameter list is where it asks. The parameter is in the type, so a reader sees from the signature what a method needs; the call site leaves it out, and the compiler finds a given of that type in the enclosing scope.
A given value and a using parameter
connect takes a query and asks for a Config in a second parameter list marked using. One given of that type sits at the top of the file, and every call supplies it without writing it:
given_using.scala
case class Config(host: String, port: Int)
given Config = Config("localhost", 8080)
def connect(query: String)(using config: Config): String =
s"$query -> ${config.host}:${config.port}"
def pause(using millis: Int): String = s"paused ${millis}ms"
given Int = 500
@main def given_using(): Unit =
println(connect("select 1"))
println(connect("select 2")(using Config("db.internal", 5432)))
println(pause)
println(summon[Config].host)
println(summon[Int])
scala-cli run --server=false given_using.scala
select 1 -> localhost:8080
select 2 -> db.internal:5432
paused 500ms
localhost
500
The second call passes its own configuration with (using Config(...)), which is how a test replaces a production value without touching the method. summon[Config] asks the compiler for the instance in scope, and summon[Int] shows that a given may be of a plain type: the 500 is what pause reads. An unnamed given such as given Config = takes its name from its type, which is enough while only one of that type exists.
The import a given needs
A given that lives in an object is not in scope until it is imported. Two given instances of one type may exist as long as only one is in scope, and the other can still be passed explicitly:
imports.scala
trait Separator:
def value: String
object Separators:
given csv: Separator = new Separator:
def value: String = ","
given pipe: Separator = new Separator:
def value: String = "|"
def render(row: List[String])(using separator: Separator): String =
row.mkString(separator.value)
import Separators.csv
@main def imports(): Unit =
println(render(List("a", "b", "c")))
println(render(List("a", "b"))(using Separators.pipe))
scala-cli run --server=false imports.scala
a,b,c
a|b
import Separators.csv imports that one given by name. A wildcard import does not bring a given at all: after import Separators.* the call fails with No given instance of type Separator, and the error explains that the instance was not imported with import given. That wider import would bring both and make the call ambiguous, which the compiler reports rather than choosing. The explicit (using Separators.pipe) needs no import at all. That is the rule to remember: a given is found by its type in the scope of the call, and nothing global is involved.
Related
- Type classes: the pattern a given is the instance of.
- Methods and parameters: the second parameter list an ordinary method may have too.
- Extension methods: the other thing an import brings into scope.
- By-name parameters: a parameter list that is evaluated later, which a using list is not.
Автор статьи: Arthur Isaev