@@ -19,11 +19,15 @@ Those parameters are called _Context Parameters_ because they are inferred by th
19
19
20
20
For instance, consider a program that sorts a list of addresses by two criteria: the city name and then street name.
21
21
22
+ {% tabs contextual_1 class=tabs-scala-version %}
23
+ {% tab 'Scala 2 and 3' for=contextual_1 %}
22
24
``` scala
23
25
val addresses : List [Address ] = ...
24
26
25
27
addresses.sortBy(address => (address.city, address.street))
26
28
```
29
+ {% endtab %}
30
+ {% endtabs %}
27
31
28
32
The ` sortBy ` method takes a function that returns, for every address, the value to compare it with the other addresses.
29
33
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
39
43
40
44
However, it is also possible to pass it explicitly:
41
45
46
+ {% tabs contextual_2 class=tabs-scala-version %}
47
+ {% tab 'Scala 2 and 3' for=contextual_2 %}
42
48
``` scala
43
49
addresses.sortBy(address => (address.city, address.street))(using Ordering .Tuple2 (Ordering .String , Ordering .String ))
44
50
```
51
+ {% endtab %}
52
+ {% endtabs %}
45
53
46
54
In this case, the ` Ordering.Tuple2(Ordering.String, Ordering.String) ` instance is exactly the one that is otherwise inferred by the compiler.
47
55
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
51
59
52
60
For more details, see the [ Contextual Abstractions chapter] [ contextual ] of this book, and also the [ Reference documentation] [ reference ] .
53
61
54
-
55
-
56
62
[ contextual] : {% link _ overviews/scala3-book/ca-contextual-abstractions-intro.md %}
57
63
[ reference] : {{ site.scala3ref }}/overview.html
0 commit comments