|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: "Implicit Parameters" |
| 4 | +--- |
| 5 | + |
| 6 | +Functional programming tends to express most dependencies as simple function parameterization. |
| 7 | +This is clean and powerful, but it sometimes leads to functions that take many parameters and |
| 8 | +call trees where the same value is passed over and over again in long call chains to many |
| 9 | +functions. Implicit parameters can help here since they enable the compiler to synthesize |
| 10 | +repetitive arguments instead of the programmer having to write them explicitly. |
| 11 | + |
| 12 | +For example, with the [default instances](./defaults.md) defined previously, |
| 13 | +a maximum function that works for any arguments for which an ordering exists can be defined as follows: |
| 14 | +```scala |
| 15 | +def max[T](x: T, y: T)(ord: Ord[T]): T = |
| 16 | + if (ord.compare(x, y) < 0) y else x |
| 17 | +``` |
| 18 | +Here, `ord` is an _implicit parameter_ introduced with a `given` clause. |
| 19 | +The `max` method can be applied as follows: |
| 20 | +```scala |
| 21 | +max(2, 3)(default intOrd) |
| 22 | +``` |
| 23 | +The `(given intOrd)` part passes `intOrd` as an argument for the `ord` parameter. But the point of |
| 24 | +implicit parameters is that this argument can also be left out (and it usually is). So the following |
| 25 | +applications are equally valid: |
| 26 | +```scala |
| 27 | +max(2, 3) |
| 28 | +max(List(1, 2, 3), Nil) |
| 29 | +``` |
| 30 | + |
| 31 | +def foo(Int?, y: Int = 2) |
| 32 | +foo(_) |
| 33 | + |
| 34 | +## Anonymous Given Clauses |
| 35 | + |
| 36 | +In many situations, the name of an implicit parameter need not be |
| 37 | +mentioned explicitly at all, since it is used only in synthesized arguments for |
| 38 | +other implicit parameters. In that case one can avoid defining a parameter name |
| 39 | +and just provide its type. Example: |
| 40 | +```scala |
| 41 | +def maximum[T](xs: List[T])(given Ord[T]): T = |
| 42 | + xs.reduceLeft(max) |
| 43 | +``` |
| 44 | +`maximum` takes an implicit parameter of type `Ord` only to pass it on as an |
| 45 | +inferred argument to `max`. The name of the parameter is left out. |
| 46 | + |
| 47 | +Generally, implicit parameters may be defined either as a full parameter list `(given p_1: T_1, ..., p_n: T_n)` or just as a sequence of types `(given T_1, ..., T_n)`. Vararg implicit parameters are not supported. |
| 48 | + |
| 49 | +## Inferring Complex Arguments |
| 50 | + |
| 51 | +Here are two other methods that have an implicit parameter of type `Ord[T]`: |
| 52 | +```scala |
| 53 | +def descending[T](given asc: Ord[T]): Ord[T] = new Ord[T] { |
| 54 | + def compare(x: T, y: T) = asc.compare(y, x) |
| 55 | +} |
| 56 | + |
| 57 | +def minimum[T](xs: List[T])(given Ord[T]) = |
| 58 | + maximum(xs)(given descending) |
| 59 | +``` |
| 60 | +The `minimum` method's right hand side passes `descending` as an explicit argument to `maximum(xs)`. |
| 61 | +With this setup, the following calls are all well-formed, and they all normalize to the last one: |
| 62 | +```scala |
| 63 | +minimum(xs) |
| 64 | +maximum(xs)(given descending) |
| 65 | +maximum(xs)(given descending(given listOrd)) |
| 66 | +maximum(xs)(given descending(given listOrd(given intOrd))) |
| 67 | +``` |
| 68 | + |
| 69 | +## Multiple Given Clauses |
| 70 | + |
| 71 | +There can be several implicit parameter clauses in a definition and implicit parameter clauses can be freely |
| 72 | +mixed with normal ones. Example: |
| 73 | +```scala |
| 74 | +def f(u: Universe)(given ctx: u.Context)(given s: ctx.Symbol, k: ctx.Kind) = ... |
| 75 | +``` |
| 76 | +Multiple given clauses are matched left-to-right in applications. Example: |
| 77 | +```scala |
| 78 | +object global extends Universe { type Context = ... } |
| 79 | +default ctx for global.Context { type Symbol = ...; type Kind = ... } |
| 80 | +default sym for ctx.Symbol |
| 81 | +default kind for ctx.Kind |
| 82 | +``` |
| 83 | +Then the following calls are all valid (and normalize to the last one) |
| 84 | +```scala |
| 85 | +f |
| 86 | +f(global) |
| 87 | +f(global)(given ctx) |
| 88 | +f(global)(given ctx)(given sym, kind) |
| 89 | +``` |
| 90 | +But `f(global)(given sym, kind)` would give a type error. |
| 91 | + |
| 92 | +## Summoning Instances |
| 93 | + |
| 94 | +The method `summon` in `Predef` returns the default of a specific type. For example, |
| 95 | +the default for `Ord[List[Int]]` is produced by |
| 96 | +```scala |
| 97 | +summon[Ord[List[Int]]] // reduces to listOrd(given intOrd) |
| 98 | +``` |
| 99 | +The `summon` method is simply defined as the (non-widening) identity function over an implicit parameter. |
| 100 | +```scala |
| 101 | +def summon[T](given x: T): x.type = x |
| 102 | +``` |
| 103 | + |
| 104 | +## Syntax |
| 105 | + |
| 106 | +Here is the new syntax of parameters and arguments seen as a delta from the [standard context free syntax of Scala 3](../../internals/syntax.md). |
| 107 | +``` |
| 108 | +ClsParamClauses ::= ... |
| 109 | + | {ClsParamClause} {GivenClsParamClause} |
| 110 | +GivenClsParamClause ::= ‘(’ ‘given’ (ClsParams | GivenTypes) ‘)’ |
| 111 | +DefParamClauses ::= ... |
| 112 | + | {DefParamClause} {GivenParamClause} |
| 113 | +GivenParamClause ::= ‘(’ ‘given’ (DefParams | GivenTypes) ‘)’ |
| 114 | +GivenTypes ::= AnnotType {‘,’ AnnotType} |
| 115 | +
|
| 116 | +ParArgumentExprs ::= ... |
| 117 | + | ‘(’ ‘given’ ExprsInParens ‘)’ |
| 118 | +``` |
0 commit comments