Skip to content

Commit f84fea6

Browse files
alvinjjulienrf
andauthored
Updated pages to be “Scala 3 Only” (#2689)
* Updated pages to be 'Scala 3 Only' * 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 * Updated 'New in Scala 3' approach --------- Co-authored-by: Julien Richard-Foy <[email protected]>
1 parent d2fc35b commit f84fea6

File tree

5 files changed

+51
-20
lines changed

5 files changed

+51
-20
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

+27-17
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,60 @@
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
---
1010

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.
1812
In that case you don’t have to define a parameter name, and can just provide the parameter type.
1913

2014

2115
## Background
2216

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

19+
{% tabs context-bounds-max-named-param class=tabs-scala-version %}
20+
21+
{% tab 'Scala 2' %}
2522
```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 =
2724
xs.reduceLeft(max(ord))
2825
```
26+
{% endtab %}
2927

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' %}
3229
```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))
3532
```
33+
{% endtab %}
3634

35+
{% endtabs %}
3736

3837
## Context bounds
3938

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.”
4140

4241
Using a context bound, the `maximum` method can be written like this:
4342

43+
{% tabs context-bounds-max-rewritten %}
44+
45+
{% tab 'Scala 2 and 3' %}
46+
4447
```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)
4650
```
4751

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.
4959

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.

_overviews/scala3-book/ca-given-imports.md

+17
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ num: 62
77
previous-page: ca-context-bounds
88
next-page: ca-type-classes
99
---
10+
<span class="tag tag-inline">Scala 3 only</span>
1011

1112

1213
To make it more clear where givens in the current scope are coming from, a special form of the `import` statement is used to import `given` instances.
1314
The basic form is shown in this example:
1415

16+
{% tabs given-imports-basic-form %}
17+
18+
{% tab 'Scala 3 Only' %}
19+
1520
```scala
1621
object A:
1722
class TC
@@ -23,15 +28,27 @@ object B:
2328
import A.given // import the given instance
2429
```
2530

31+
{% endtab %}
32+
33+
{% endtabs %}
34+
2635
In this code the `import A.*` clause of object `B` imports all members of `A` *except* the `given` instance, `tc`.
2736
Conversely, the second import, `import A.given`, imports *only* that `given` instance.
2837
The two `import` clauses can also be merged into one:
2938

39+
{% tabs given-imports-merged %}
40+
41+
{% tab 'Scala 3 Only' %}
42+
3043
```scala
3144
object B:
3245
import A.{given, *}
3346
```
3447

48+
{% endtab %}
49+
50+
{% endtabs %}
51+
3552

3653
## Discussion
3754

_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`.

_overviews/scala3-book/ca-multiversal-equality.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ num: 64
77
previous-page: ca-type-classes
88
next-page: ca-implicit-conversions
99
---
10+
<span class="tag tag-inline">New In Scala 3</span>
1011

12+
> Multiversal Equality is a new language feature that was introduced in Scala 3.
13+
> Because it has no equivalent in Scala 2, all code examples
14+
> in this lesson assume you are using Scala 3.
1115
1216
Previously, Scala had *universal equality*: Two values of any types could be compared with each other using `==` and `!=`.
1317
This came from the fact that `==` and `!=` are implemented in terms of Java’s `equals` method, which can also compare values of any two reference types.

0 commit comments

Comments
 (0)