Skip to content

add code tabs in num14. #2587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 18, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions _overviews/scala3-book/taste-contextual-abstractions.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ Those parameters are called _Context Parameters_ because they are inferred by th

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

{% tabs contextual_1 %}
{% tab 'Scala 2 and 3' for=contextual_1 %}

```scala
val addresses: List[Address] = ...

addresses.sortBy(address => (address.city, address.street))
```

{% endtab %}
{% endtabs %}

The `sortBy` method takes a function that returns, for every address, the value to compare it with the other addresses.
In this case, we pass a function that returns a pair containing the city name and the street name.

Expand All @@ -39,10 +45,16 @@ It is convenient to omit it because we know `String`s are generally compared usi

However, it is also possible to pass it explicitly:

{% tabs contextual_2 %}
{% tab 'Scala 2 and 3' for=contextual_2 %}

```scala
addresses.sortBy(address => (address.city, address.street))(using Ordering.Tuple2(Ordering.String, Ordering.String))
```

{% endtab %}
{% endtabs %}

In this case, the `Ordering.Tuple2(Ordering.String, Ordering.String)` instance is exactly the one that is otherwise inferred by the compiler.
In other words both examples produce the same program.

Expand All @@ -51,7 +63,5 @@ They help developers write pieces of code that are extensible and concise at the

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



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