From 366c71b384dd408f53bc5d12becf9bb89ba7f7a3 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Thu, 6 Oct 2022 19:49:14 +0800 Subject: [PATCH 1/5] add code tabs in num14. --- .../scala3-book/taste-contextual-abstractions.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/_overviews/scala3-book/taste-contextual-abstractions.md b/_overviews/scala3-book/taste-contextual-abstractions.md index c1db055e9e..75a8e1fa92 100644 --- a/_overviews/scala3-book/taste-contextual-abstractions.md +++ b/_overviews/scala3-book/taste-contextual-abstractions.md @@ -19,11 +19,15 @@ 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 class=tabs-scala-version %} +{% 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. @@ -39,9 +43,13 @@ 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 class=tabs-scala-version %} +{% 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. @@ -51,7 +59,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 From 6c5e4dd609c38bf1d28a40cab8eb6266ea6f931b Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Fri, 7 Oct 2022 23:28:30 +0800 Subject: [PATCH 2/5] Update _overviews/scala3-book/taste-contextual-abstractions.md Co-authored-by: Jamie Thompson --- _overviews/scala3-book/taste-contextual-abstractions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-contextual-abstractions.md b/_overviews/scala3-book/taste-contextual-abstractions.md index 75a8e1fa92..80c6e8a395 100644 --- a/_overviews/scala3-book/taste-contextual-abstractions.md +++ b/_overviews/scala3-book/taste-contextual-abstractions.md @@ -19,7 +19,7 @@ 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 class=tabs-scala-version %} +{% tabs contextual_1 %} {% tab 'Scala 2 and 3' for=contextual_1 %} ```scala val addresses: List[Address] = ... From a1ba4fd1059f004c67908b8b8746a32a11fe4ccf Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Fri, 7 Oct 2022 23:33:22 +0800 Subject: [PATCH 3/5] correct 2&3 and 3 only. --- _overviews/scala3-book/taste-contextual-abstractions.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-contextual-abstractions.md b/_overviews/scala3-book/taste-contextual-abstractions.md index 80c6e8a395..bedb562ca8 100644 --- a/_overviews/scala3-book/taste-contextual-abstractions.md +++ b/_overviews/scala3-book/taste-contextual-abstractions.md @@ -21,11 +21,13 @@ For instance, consider a program that sorts a list of addresses by two criteria: {% 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 %} @@ -43,11 +45,13 @@ 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 class=tabs-scala-version %} +{% 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 %} From 1aea632ff22c7585d299b518582928e837b984c0 Mon Sep 17 00:00:00 2001 From: Ben Luo Date: Sat, 15 Oct 2022 02:22:21 +0800 Subject: [PATCH 4/5] change to scala3 only. --- _overviews/scala3-book/taste-contextual-abstractions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_overviews/scala3-book/taste-contextual-abstractions.md b/_overviews/scala3-book/taste-contextual-abstractions.md index bedb562ca8..971b1a5ca0 100644 --- a/_overviews/scala3-book/taste-contextual-abstractions.md +++ b/_overviews/scala3-book/taste-contextual-abstractions.md @@ -46,7 +46,7 @@ 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 %} +{% tab 'Scala 3 Only' for=contextual_2 %} ```scala addresses.sortBy(address => (address.city, address.street))(using Ordering.Tuple2(Ordering.String, Ordering.String)) From f967ea75b913f36a78af31dc16b938609cf96a90 Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Tue, 18 Oct 2022 17:24:37 +0200 Subject: [PATCH 5/5] adjust formatting --- .../scala3-book/taste-contextual-abstractions.md | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/_overviews/scala3-book/taste-contextual-abstractions.md b/_overviews/scala3-book/taste-contextual-abstractions.md index 971b1a5ca0..9e25f5029a 100644 --- a/_overviews/scala3-book/taste-contextual-abstractions.md +++ b/_overviews/scala3-book/taste-contextual-abstractions.md @@ -45,13 +45,22 @@ 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 3 Only' for=contextual_2 %} +{% tabs contextual_2 class=tabs-scala-version %} +{% tab 'Scala 2' for=contextual_2 %} + +```scala +addresses.sortBy(address => (address.city, address.street))(Ordering.Tuple2(Ordering.String, Ordering.String)) +``` + +{% endtab %} +{% tab 'Scala 3' for=contextual_2 %} ```scala addresses.sortBy(address => (address.city, address.street))(using Ordering.Tuple2(Ordering.String, Ordering.String)) ``` +in Scala 3 `using` in an argument list to `sortBy` signals passing the context parameter explicitly, avoiding ambiguity. + {% endtab %} {% endtabs %}