Skip to content

Commit 24d5606

Browse files
committed
Cross-write page Context Bounds
- remove tag “Scala 3 only” - add a link to the page that explains context parameters and inferred terms - remove paragraph that shows anonymous context parameters - fix link to the FAQ - also cross-write the FAQ entry
1 parent 51994bb commit 24d5606

File tree

3 files changed

+16
-33
lines changed

3 files changed

+16
-33
lines changed

_overviews/FAQ/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ See [this]({{ site.baseurl }}/tutorials/FAQ/initialization-order.html).
149149

150150
See the [Scala 2.13 Collections Guide](https://docs.scala-lang.org/overviews/collections-2.13/introduction.html).
151151

152-
### What are context bounds (`[T : Foo]`)?
152+
### What are context bounds?
153153

154-
It's syntactic sugar for an `implicit` parameter of type `Foo[T]`.
154+
It's syntactic sugar for a context parameter (an `implicit` parameter in Scala 2, or a `using` parameter in Scala 3).
155155

156156
More details in this [Stack Overflow answer](https://stackoverflow.com/a/4467012).
157157

+13-30
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,48 @@
11
---
22
title: Context Bounds
33
type: section
4-
description: This page demonstrates Context Bounds in Scala 3.
4+
description: This page demonstrates Context Bounds in Scala.
55
languages: [zh-cn]
66
num: 61
77
previous-page: ca-given-using-clauses
88
next-page: ca-given-imports
99
---
10-
<span class="tag tag-inline">Scala 3 only</span>
1110

12-
13-
{% comment %}
14-
- TODO: define "context parameter"
15-
- TODO: define "synthesized" and "synthesized arguments"
16-
{% endcomment %}
17-
18-
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.
1912
In that case you don’t have to define a parameter name, and can just provide the parameter type.
2013

2114

2215
## Background
2316

2417
For example, this `maximum` method takes a _context parameter_ of type `Ord`, only to pass it on as an argument to `max`:
2518

26-
{% tabs context-bounds-max-named-param %}
27-
28-
{% tab 'Scala 3 Only' %}
19+
{% tabs context-bounds-max-named-param class=tabs-scala-version %}
2920

21+
{% tab 'Scala 2' %}
3022
```scala
31-
def maximum[A](xs: List[A])(using ord: Ord[A]): A =
23+
def maximum[A](xs: List[A])(implicit ord: Ord[A]): A =
3224
xs.reduceLeft(max(ord))
3325
```
34-
3526
{% endtab %}
3627

37-
{% endtabs %}
38-
39-
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:
40-
41-
{% tabs context-bounds-no-param-name %}
42-
43-
{% tab 'Scala 3 Only' %}
44-
28+
{% tab 'Scala 3' %}
4529
```scala
46-
def maximum[A](xs: List[A])(using Ord[A]): A =
47-
xs.reduceLeft(max)
30+
def maximum[A](xs: List[A])(using ord: Ord[A]): A =
31+
xs.reduceLeft(max(ord))
4832
```
49-
5033
{% endtab %}
5134

5235
{% endtabs %}
5336

54-
5537
## Context bounds
5638

57-
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.”
5840

5941
Using a context bound, the `maximum` method can be written like this:
6042

6143
{% tabs context-bounds-max-rewritten %}
6244

63-
{% tab 'Scala 3 Only' %}
45+
{% tab 'Scala 2 and 3' %}
6446

6547
```scala
6648
def maximum[A: Ord](xs: List[A]): A = xs.reduceLeft(max)
@@ -71,6 +53,7 @@ def maximum[A: Ord](xs: List[A]): A = xs.reduceLeft(max)
7153
{% endtabs %}
7254

7355

74-
A bound like `: Ord` on a type parameter `A` of a method or class indicates a context parameter with `Ord[A]`.
56+
A bound like `: Ord` on a type parameter `A` of a method or class indicates a context parameter with type `Ord[A]`.
57+
Under the hood, the compiler transforms this syntax into the one shown in the Background section.
7558

76-
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.
59+
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.

_overviews/scala3-book/ca-given-using-clauses.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def renderWidget(items: List[String])(using c: Config): String = ???
6363
{% endtab %}
6464
{% endtabs %}
6565

66-
By starting a parameter section with the keyword `using`, we tell the Scala compiler that at the callsite it should automatically find an argument with the correct type.
66+
By starting a parameter section with the keyword `using`, we tell the Scala compiler that at the call-site it should automatically find an argument with the correct type.
6767
The Scala compiler thus performs **term inference**.
6868

6969
In our call to `renderWidget(List("cart"))` the Scala compiler will see that there is a term of type `Config` in scope (the `c`) and automatically provide it to `renderWidget`.

0 commit comments

Comments
 (0)