|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: "Given Instances" |
| 4 | +--- |
| 5 | + |
| 6 | +Given instances (or, simply, "givens") define "canonical" values of certain types |
| 7 | +that serve for synthesizing arguments to [given clauses](./given-clauses.md). Example: |
| 8 | + |
| 9 | +```scala |
| 10 | +trait Ord[T] { |
| 11 | + def compare(x: T, y: T): Int |
| 12 | + def (x: T) < (y: T) = compare(x, y) < 0 |
| 13 | + def (x: T) > (y: T) = compare(x, y) > 0 |
| 14 | +} |
| 15 | + |
| 16 | +given intOrd: Ord[Int] { |
| 17 | + def compare(x: Int, y: Int) = |
| 18 | + if (x < y) -1 else if (x > y) +1 else 0 |
| 19 | +} |
| 20 | + |
| 21 | +given listOrd[T](given ord: Ord[T]): Ord[List[T]] { |
| 22 | + |
| 23 | + def compare(xs: List[T], ys: List[T]): Int = (xs, ys) match { |
| 24 | + case (Nil, Nil) => 0 |
| 25 | + case (Nil, _) => -1 |
| 26 | + case (_, Nil) => +1 |
| 27 | + case (x :: xs1, y :: ys1) => |
| 28 | + val fst = ord.compare(x, y) |
| 29 | + if (fst != 0) fst else xs1.compareTo(ys1) |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | +This code defines a trait `Ord` with two given instances. `intOrd` defines |
| 34 | +a given for the type `Ord[Int]` whereas `listOrd[T]` defines givens |
| 35 | +for `Ord[List[T]]` for all types `T` that come with a given instance for `Ord[T]` themselves. |
| 36 | +The `(given ord: Ord[T])` clause in `listOrd` defines an implicit parameter. |
| 37 | +Given clauses are further explained in the [next section](./given-clauses.md). |
| 38 | + |
| 39 | +## Anonymous Given Instances |
| 40 | + |
| 41 | +The name of a given instance can be left out. So the definitions |
| 42 | +of the last section can also be expressed like this: |
| 43 | +```scala |
| 44 | +given Ord[Int] { ... } |
| 45 | +given [T](given Ord[T]): Ord[List[T]] { ... } |
| 46 | +``` |
| 47 | +If the name of a given is missing, the compiler will synthesize a name from |
| 48 | +the type(s) in the `as` clause. |
| 49 | + |
| 50 | +## Alias Givens |
| 51 | + |
| 52 | +An alias can be used to define a given instance that is equal to some expression. E.g.: |
| 53 | +```scala |
| 54 | +given global: ExecutionContext = new ForkJoinPool() |
| 55 | +``` |
| 56 | +This creates a given `global` of type `ExecutionContext` that resolves to the right |
| 57 | +hand side `new ForkJoinPool()`. |
| 58 | +The first time `global` is accessed, a new `ForkJoinPool` is created, which is then |
| 59 | +returned for this and all subsequent accesses to `global`. |
| 60 | + |
| 61 | +Alias givens can be anonymous, e.g. |
| 62 | +```scala |
| 63 | +given Position = enclosingTree.position |
| 64 | +given (given outer: Context): Context = outer.withOwner(currentOwner) |
| 65 | +``` |
| 66 | +An alias given can have type parameters and given clauses just like any other given instance, but it can only implement a single type. |
| 67 | + |
| 68 | +## Given Instance Initialization |
| 69 | + |
| 70 | +A given instance without type parameters or given clause is initialized on-demand, the first |
| 71 | +time it is accessed. It is not required to ensure safe publication, which means that |
| 72 | +different threads might create different instances for the same `given` definition. |
| 73 | +If a `given` definition has type parameters or a given clause, a fresh instance is created for each reference. |
| 74 | + |
| 75 | +## Syntax |
| 76 | + |
| 77 | +Here is the new syntax of given instances, seen as a delta from the [standard context free syntax of Scala 3](../../internals/syntax.md). |
| 78 | +``` |
| 79 | +TmplDef ::= ... |
| 80 | + | ‘given’ GivenDef |
| 81 | +GivenDef ::= [GivenSig (‘:’ | <:)] Type ‘=’ Expr |
| 82 | + | [GivenSig ‘:’] [ConstrApp {‘,’ ConstrApp }] [TemplateBody] |
| 83 | +GivenSig ::= [id] [DefTypeParamClause] {GivenParamClause} |
| 84 | +GivenParamClause ::= ‘(’ ‘given’ (DefParams | GivenTypes) ‘)’ |
| 85 | +GivenTypes ::= AnnotType {‘,’ AnnotType} |
| 86 | +``` |
| 87 | +The identifier `id` can be omitted only if some types are implemented or the template body defines at least one extension method. |
0 commit comments