Skip to content

Commit 366c71b

Browse files
committed
add code tabs in num14.
1 parent 69a362b commit 366c71b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

_overviews/scala3-book/taste-contextual-abstractions.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ Those parameters are called _Context Parameters_ because they are inferred by th
1919

2020
For instance, consider a program that sorts a list of addresses by two criteria: the city name and then street name.
2121

22+
{% tabs contextual_1 class=tabs-scala-version %}
23+
{% tab 'Scala 2 and 3' for=contextual_1 %}
2224
```scala
2325
val addresses: List[Address] = ...
2426

2527
addresses.sortBy(address => (address.city, address.street))
2628
```
29+
{% endtab %}
30+
{% endtabs %}
2731

2832
The `sortBy` method takes a function that returns, for every address, the value to compare it with the other addresses.
2933
In this case, we pass a function that returns a pair containing the city name and the street name.
@@ -39,9 +43,13 @@ It is convenient to omit it because we know `String`s are generally compared usi
3943

4044
However, it is also possible to pass it explicitly:
4145

46+
{% tabs contextual_2 class=tabs-scala-version %}
47+
{% tab 'Scala 2 and 3' for=contextual_2 %}
4248
```scala
4349
addresses.sortBy(address => (address.city, address.street))(using Ordering.Tuple2(Ordering.String, Ordering.String))
4450
```
51+
{% endtab %}
52+
{% endtabs %}
4553

4654
In this case, the `Ordering.Tuple2(Ordering.String, Ordering.String)` instance is exactly the one that is otherwise inferred by the compiler.
4755
In other words both examples produce the same program.
@@ -51,7 +59,5 @@ They help developers write pieces of code that are extensible and concise at the
5159

5260
For more details, see the [Contextual Abstractions chapter][contextual] of this book, and also the [Reference documentation][reference].
5361

54-
55-
5662
[contextual]: {% link _overviews/scala3-book/ca-contextual-abstractions-intro.md %}
5763
[reference]: {{ site.scala3ref }}/overview.html

0 commit comments

Comments
 (0)