|
1 | 1 | ---
|
2 | 2 | title: Context Bounds
|
3 | 3 | type: section
|
4 |
| -description: This page demonstrates Context Bounds in Scala 3. |
| 4 | +description: This page demonstrates Context Bounds in Scala. |
5 | 5 | languages: [zh-cn]
|
6 | 6 | num: 61
|
7 | 7 | previous-page: ca-given-using-clauses
|
8 | 8 | next-page: ca-given-imports
|
9 | 9 | ---
|
10 | 10 |
|
11 |
| - |
12 |
| -{% comment %} |
13 |
| -- TODO: define "context parameter" |
14 |
| -- TODO: define "synthesized" and "synthesized arguments" |
15 |
| -{% endcomment %} |
16 |
| - |
17 |
| -In many situations the name of a _context parameter_ doesn’t have to be mentioned explicitly, since it’s only used by the compiler in synthesized arguments for other context parameters. |
| 11 | +In many situations the name of a [context parameter]({% link _overviews/scala3-book/ca-given-using-clauses.md %}#using-clauses) doesn’t have to be mentioned explicitly, since it’s only used by the compiler in synthesized arguments for other context parameters. |
18 | 12 | In that case you don’t have to define a parameter name, and can just provide the parameter type.
|
19 | 13 |
|
20 | 14 |
|
21 | 15 | ## Background
|
22 | 16 |
|
23 | 17 | For example, this `maximum` method takes a _context parameter_ of type `Ord`, only to pass it on as an argument to `max`:
|
24 | 18 |
|
| 19 | +{% tabs context-bounds-max-named-param class=tabs-scala-version %} |
| 20 | + |
| 21 | +{% tab 'Scala 2' %} |
25 | 22 | ```scala
|
26 |
| -def maximum[A](xs: List[A])(using ord: Ord[A]): A = |
| 23 | +def maximum[A](xs: List[A])(implicit ord: Ord[A]): A = |
27 | 24 | xs.reduceLeft(max(ord))
|
28 | 25 | ```
|
| 26 | +{% endtab %} |
29 | 27 |
|
30 |
| -In that code the parameter name `ord` isn’t actually required; it can be passed on as an inferred argument to `max`, so you just state that `maximum` uses the type `Ord[A]` without giving it a name: |
31 |
| - |
| 28 | +{% tab 'Scala 3' %} |
32 | 29 | ```scala
|
33 |
| -def maximum[A](xs: List[A])(using Ord[A]): A = |
34 |
| - xs.reduceLeft(max) |
| 30 | +def maximum[A](xs: List[A])(using ord: Ord[A]): A = |
| 31 | + xs.reduceLeft(max(using ord)) |
35 | 32 | ```
|
| 33 | +{% endtab %} |
36 | 34 |
|
| 35 | +{% endtabs %} |
37 | 36 |
|
38 | 37 | ## Context bounds
|
39 | 38 |
|
40 |
| -Given that background, a _context bound_ is a shorthand syntax for expressing the pattern of, “a context parameter that depends on a type parameter.” |
| 39 | +Given that background, a _context bound_ is a shorthand syntax for expressing the pattern of, “a context parameter applied to a type parameter.” |
41 | 40 |
|
42 | 41 | Using a context bound, the `maximum` method can be written like this:
|
43 | 42 |
|
| 43 | +{% tabs context-bounds-max-rewritten %} |
| 44 | + |
| 45 | +{% tab 'Scala 2 and 3' %} |
| 46 | + |
44 | 47 | ```scala
|
45 |
| -def maximum[A: Ord](xs: List[A]): A = xs.reduceLeft(max) |
| 48 | +def maximum[A: Ord](xs: List[A]): A = |
| 49 | + xs.reduceLeft(max) |
46 | 50 | ```
|
47 | 51 |
|
48 |
| -A bound like `: Ord` on a type parameter `A` of a method or class indicates a context parameter with `Ord[A]`. |
| 52 | +{% endtab %} |
| 53 | + |
| 54 | +{% endtabs %} |
| 55 | + |
| 56 | + |
| 57 | +A bound like `: Ord` on a type parameter `A` of a method or class indicates a context parameter with type `Ord[A]`. |
| 58 | +Under the hood, the compiler transforms this syntax into the one shown in the Background section. |
49 | 59 |
|
50 |
| -For more information about context bounds, see the [“What are context bounds?”](https://docs.scala-lang.org/tutorials/FAQ/context-bounds.html) section of the Scala FAQ. |
| 60 | +For more information about context bounds, see the [“What are context bounds?”]({% link _overviews/FAQ/index.md %}#what-are-context-bounds) section of the Scala FAQ. |
0 commit comments