From 3d80ecd72146d3d8e91ba47d99abb2514ecfe843 Mon Sep 17 00:00:00 2001 From: gkepka Date: Tue, 10 Dec 2024 13:49:39 +0100 Subject: [PATCH 1/4] Scala 2 Book migration - Control Structures --- _overviews/scala3-book/ca-context-bounds.md | 2 +- .../scala3-book/ca-context-parameters.md | 2 +- .../ca-contextual-abstractions-intro.md | 2 +- .../scala3-book/ca-extension-methods.md | 2 +- _overviews/scala3-book/ca-given-imports.md | 2 +- .../scala3-book/ca-implicit-conversions.md | 2 +- .../scala3-book/ca-multiversal-equality.md | 2 +- _overviews/scala3-book/ca-summary.md | 2 +- _overviews/scala3-book/ca-type-classes.md | 2 +- _overviews/scala3-book/collections-classes.md | 2 +- _overviews/scala3-book/collections-intro.md | 2 +- _overviews/scala3-book/collections-methods.md | 2 +- _overviews/scala3-book/collections-summary.md | 2 +- _overviews/scala3-book/concurrency.md | 2 +- _overviews/scala3-book/control-structures.md | 59 +++++++++++-- _overviews/scala3-book/fun-eta-expansion.md | 2 +- .../scala3-book/fun-function-variables.md | 2 +- _overviews/scala3-book/fun-hofs.md | 2 +- _overviews/scala3-book/fun-intro.md | 2 +- .../scala3-book/fun-partial-functions.md | 82 +++++++++++++++++++ _overviews/scala3-book/fun-summary.md | 2 +- .../scala3-book/fun-write-map-function.md | 2 +- .../fun-write-method-returns-function.md | 2 +- .../scala3-book/interacting-with-java.md | 2 +- _overviews/scala3-book/packaging-imports.md | 2 +- _overviews/scala3-book/scala-for-java-devs.md | 2 +- .../scala3-book/scala-for-javascript-devs.md | 2 +- .../scala3-book/scala-for-python-devs.md | 2 +- _overviews/scala3-book/scala-tools.md | 2 +- _overviews/scala3-book/tools-sbt.md | 2 +- _overviews/scala3-book/tools-worksheets.md | 2 +- _overviews/scala3-book/types-adts-gadts.md | 2 +- .../scala3-book/types-dependent-function.md | 2 +- _overviews/scala3-book/types-generics.md | 2 +- _overviews/scala3-book/types-inferred.md | 2 +- _overviews/scala3-book/types-intersection.md | 2 +- _overviews/scala3-book/types-introduction.md | 2 +- _overviews/scala3-book/types-opaque-types.md | 2 +- _overviews/scala3-book/types-others.md | 2 +- _overviews/scala3-book/types-structural.md | 2 +- _overviews/scala3-book/types-union.md | 2 +- _overviews/scala3-book/types-variance.md | 2 +- _overviews/scala3-book/where-next.md | 2 +- 43 files changed, 174 insertions(+), 49 deletions(-) create mode 100644 _overviews/scala3-book/fun-partial-functions.md diff --git a/_overviews/scala3-book/ca-context-bounds.md b/_overviews/scala3-book/ca-context-bounds.md index 2d36d7b3c..d4346ed94 100644 --- a/_overviews/scala3-book/ca-context-bounds.md +++ b/_overviews/scala3-book/ca-context-bounds.md @@ -3,7 +3,7 @@ title: Context Bounds type: section description: This page demonstrates Context Bounds in Scala. languages: [ru, zh-cn] -num: 62 +num: 63 previous-page: ca-context-parameters next-page: ca-given-imports --- diff --git a/_overviews/scala3-book/ca-context-parameters.md b/_overviews/scala3-book/ca-context-parameters.md index d91a3c0cc..d0ea35041 100644 --- a/_overviews/scala3-book/ca-context-parameters.md +++ b/_overviews/scala3-book/ca-context-parameters.md @@ -3,7 +3,7 @@ title: Context Parameters type: section description: This page demonstrates how to declare context parameters, and how the compiler infers them at call-site. languages: [ru, zh-cn] -num: 61 +num: 62 previous-page: ca-extension-methods next-page: ca-context-bounds redirect_from: /scala3/book/ca-given-using-clauses.html diff --git a/_overviews/scala3-book/ca-contextual-abstractions-intro.md b/_overviews/scala3-book/ca-contextual-abstractions-intro.md index c6ec74ec1..8f7f5f79a 100644 --- a/_overviews/scala3-book/ca-contextual-abstractions-intro.md +++ b/_overviews/scala3-book/ca-contextual-abstractions-intro.md @@ -3,7 +3,7 @@ title: Contextual Abstractions type: chapter description: This chapter provides an introduction to the Scala 3 concept of Contextual Abstractions. languages: [ru, zh-cn] -num: 59 +num: 60 previous-page: types-others next-page: ca-extension-methods --- diff --git a/_overviews/scala3-book/ca-extension-methods.md b/_overviews/scala3-book/ca-extension-methods.md index 9f54297f9..49f07b45b 100644 --- a/_overviews/scala3-book/ca-extension-methods.md +++ b/_overviews/scala3-book/ca-extension-methods.md @@ -3,7 +3,7 @@ title: Extension Methods type: section description: This page demonstrates how Extension Methods work in Scala 3. languages: [ru, zh-cn] -num: 60 +num: 61 previous-page: ca-contextual-abstractions-intro next-page: ca-context-parameters scala3: true diff --git a/_overviews/scala3-book/ca-given-imports.md b/_overviews/scala3-book/ca-given-imports.md index 97ddde337..bc7c0754f 100644 --- a/_overviews/scala3-book/ca-given-imports.md +++ b/_overviews/scala3-book/ca-given-imports.md @@ -3,7 +3,7 @@ title: Given Imports type: section description: This page demonstrates how 'given' import statements work in Scala 3. languages: [ru, zh-cn] -num: 63 +num: 64 previous-page: ca-context-bounds next-page: ca-type-classes scala3: true diff --git a/_overviews/scala3-book/ca-implicit-conversions.md b/_overviews/scala3-book/ca-implicit-conversions.md index 0fb665eda..2c2884aa5 100644 --- a/_overviews/scala3-book/ca-implicit-conversions.md +++ b/_overviews/scala3-book/ca-implicit-conversions.md @@ -3,7 +3,7 @@ title: Implicit Conversions type: section description: This page demonstrates how to implement Implicit Conversions in Scala. languages: [ru, zh-cn] -num: 66 +num: 67 previous-page: ca-multiversal-equality next-page: ca-summary --- diff --git a/_overviews/scala3-book/ca-multiversal-equality.md b/_overviews/scala3-book/ca-multiversal-equality.md index 062f23976..dfc6b4cdb 100644 --- a/_overviews/scala3-book/ca-multiversal-equality.md +++ b/_overviews/scala3-book/ca-multiversal-equality.md @@ -3,7 +3,7 @@ title: Multiversal Equality type: section description: This page demonstrates how to implement Multiversal Equality in Scala 3. languages: [ru, zh-cn] -num: 65 +num: 66 previous-page: ca-type-classes next-page: ca-implicit-conversions scala3: true diff --git a/_overviews/scala3-book/ca-summary.md b/_overviews/scala3-book/ca-summary.md index f17ffc035..bdd8c5853 100644 --- a/_overviews/scala3-book/ca-summary.md +++ b/_overviews/scala3-book/ca-summary.md @@ -3,7 +3,7 @@ title: Summary type: section description: This page provides a summary of the Contextual Abstractions lessons. languages: [ru, zh-cn] -num: 67 +num: 68 previous-page: ca-implicit-conversions next-page: concurrency --- diff --git a/_overviews/scala3-book/ca-type-classes.md b/_overviews/scala3-book/ca-type-classes.md index 71af01513..2a56a5de4 100644 --- a/_overviews/scala3-book/ca-type-classes.md +++ b/_overviews/scala3-book/ca-type-classes.md @@ -3,7 +3,7 @@ title: Type Classes type: section description: This page demonstrates how to create and use type classes. languages: [ru, zh-cn] -num: 64 +num: 65 previous-page: ca-given-imports next-page: ca-multiversal-equality redirect_from: /scala3/book/types-type-classes.html diff --git a/_overviews/scala3-book/collections-classes.md b/_overviews/scala3-book/collections-classes.md index 8a91c0507..acf3a7ff8 100644 --- a/_overviews/scala3-book/collections-classes.md +++ b/_overviews/scala3-book/collections-classes.md @@ -3,7 +3,7 @@ title: Collections Types type: section description: This page introduces the common Scala 3 collections types and some of their methods. languages: [ru, zh-cn] -num: 38 +num: 39 previous-page: collections-intro next-page: collections-methods --- diff --git a/_overviews/scala3-book/collections-intro.md b/_overviews/scala3-book/collections-intro.md index 9d99900c0..e953b9530 100644 --- a/_overviews/scala3-book/collections-intro.md +++ b/_overviews/scala3-book/collections-intro.md @@ -3,7 +3,7 @@ title: Scala Collections type: chapter description: This page provides and introduction to the common collections classes and their methods in Scala 3. languages: [ru, zh-cn] -num: 37 +num: 38 previous-page: packaging-imports next-page: collections-classes --- diff --git a/_overviews/scala3-book/collections-methods.md b/_overviews/scala3-book/collections-methods.md index c805aac63..6a56814b5 100644 --- a/_overviews/scala3-book/collections-methods.md +++ b/_overviews/scala3-book/collections-methods.md @@ -3,7 +3,7 @@ title: Collections Methods type: section description: This page demonstrates the common methods on the Scala 3 collections classes. languages: [ru, zh-cn] -num: 39 +num: 40 previous-page: collections-classes next-page: collections-summary --- diff --git a/_overviews/scala3-book/collections-summary.md b/_overviews/scala3-book/collections-summary.md index 0112990df..4a7aa1c38 100644 --- a/_overviews/scala3-book/collections-summary.md +++ b/_overviews/scala3-book/collections-summary.md @@ -3,7 +3,7 @@ title: Summary type: section description: This page provides a summary of the Collections chapter. languages: [ru, zh-cn] -num: 40 +num: 41 previous-page: collections-methods next-page: fp-intro --- diff --git a/_overviews/scala3-book/concurrency.md b/_overviews/scala3-book/concurrency.md index ef89e0a82..4364239bd 100644 --- a/_overviews/scala3-book/concurrency.md +++ b/_overviews/scala3-book/concurrency.md @@ -3,7 +3,7 @@ title: Concurrency type: chapter description: This page discusses how Scala concurrency works, with an emphasis on Scala Futures. languages: [ru, zh-cn] -num: 68 +num: 69 previous-page: ca-summary next-page: scala-tools --- diff --git a/_overviews/scala3-book/control-structures.md b/_overviews/scala3-book/control-structures.md index b982cf654..a6b2ad6ad 100644 --- a/_overviews/scala3-book/control-structures.md +++ b/_overviews/scala3-book/control-structures.md @@ -812,7 +812,7 @@ speak(Person("Bam Bam")) // "Bam Bam says, Bam bam!" #### Binding matched patterns to variables -You can bind the matched pattern to a variable to use type-specific behavior. +You can bind the matched pattern to a variable to use type-specific behavior: {% tabs pattern-binding class=tabs-scala-version %} {% tab 'Scala 2' for=pattern-binding %} @@ -992,10 +992,53 @@ def pattern(x: Matchable): String = x match {% endtab %} {% endtabs %} -{% comment %} -TODO: Add in the new Scala 3 syntax shown on this page: -http://dotty.epfl.ch/docs/reference/changed-features/match-syntax.html -{% endcomment %} +You can also write the code on the right side of the `=>` on multiple lines if you think is easier to read. Here’s one example: + +{% tabs control-structures-31 class=tabs-scala-version %} +{% tab 'Scala 2' for=control-structures-31 %} +```scala +count match { + case 1 => + println("one, a lonely number") + case x if x == 2 || x == 3 => + println("two's company, three's a crowd") + case x if x > 3 => + println("4+, that's a party") + case _ => + println("i'm guessing your number is zero or less") +} +``` +{% endtab %} +{% tab 'Scala 3' for=control-structures-31 %} +```scala +count match + case 1 => + println("one, a lonely number") + case x if x == 2 || x == 3 => + println("two's company, three's a crowd") + case x if x > 3 => + println("4+, that's a party") + case _ => + println("i'm guessing your number is zero or less") +``` +{% endtab %} +{% endtabs %} + +In Scala 3, `match` expressions can be chained: + +{% tabs 'control-structures-32' %} +{% tab 'Scala 3 Only' %} +```scala +i match + case odd: Int if odd % 2 == 1 => "odd" + case even: Int if even % 2 == 0 => "even" + case _ => "not an integer" +match + case "even" => true + case _ => false +``` +{% endtab %} +{% endtabs %} ## try/catch/finally @@ -1004,8 +1047,8 @@ For consistency, Scala uses the same syntax that `match` expressions use and sup In the following example, `openAndReadAFile` is a method that does what its name implies: it opens a file and reads the text in it, assigning the result to the mutable variable `text`: -{% tabs control-structures-31 class=tabs-scala-version %} -{% tab 'Scala 2' for=control-structures-31 %} +{% tabs control-structures-33 class=tabs-scala-version %} +{% tab 'Scala 2' for=control-structures-33 %} ```scala var text = "" try { @@ -1019,7 +1062,7 @@ try { } ``` {% endtab %} -{% tab 'Scala 3' for=control-structures-31 %} +{% tab 'Scala 3' for=control-structures-33 %} ```scala var text = "" try diff --git a/_overviews/scala3-book/fun-eta-expansion.md b/_overviews/scala3-book/fun-eta-expansion.md index 0854c0977..a435a4284 100644 --- a/_overviews/scala3-book/fun-eta-expansion.md +++ b/_overviews/scala3-book/fun-eta-expansion.md @@ -3,7 +3,7 @@ title: Eta-Expansion type: section description: This page discusses Eta-Expansion, the Scala technology that automatically and transparently converts methods into functions. languages: [ru, zh-cn] -num: 31 +num: 32 previous-page: fun-function-variables next-page: fun-hofs --- diff --git a/_overviews/scala3-book/fun-function-variables.md b/_overviews/scala3-book/fun-function-variables.md index 04c612b7b..248a334ed 100644 --- a/_overviews/scala3-book/fun-function-variables.md +++ b/_overviews/scala3-book/fun-function-variables.md @@ -5,7 +5,7 @@ description: This page shows how to use function variables in Scala. languages: [ru, zh-cn] num: 30 previous-page: fun-anonymous-functions -next-page: fun-eta-expansion +next-page: fun-partial-functions --- diff --git a/_overviews/scala3-book/fun-hofs.md b/_overviews/scala3-book/fun-hofs.md index 513c520a5..943845cfc 100644 --- a/_overviews/scala3-book/fun-hofs.md +++ b/_overviews/scala3-book/fun-hofs.md @@ -3,7 +3,7 @@ title: Higher-Order Functions type: section description: This page demonstrates how to create and use higher-order functions in Scala. languages: [ru, zh-cn] -num: 32 +num: 33 previous-page: fun-eta-expansion next-page: fun-write-map-function --- diff --git a/_overviews/scala3-book/fun-intro.md b/_overviews/scala3-book/fun-intro.md index a3c82e51c..66cb6bad8 100644 --- a/_overviews/scala3-book/fun-intro.md +++ b/_overviews/scala3-book/fun-intro.md @@ -10,5 +10,5 @@ next-page: fun-anonymous-functions Where the previous chapter introduced Scala *methods*, this chapter digs into *functions*. -The topics that are covered include anonymous functions, function variables, and higher-order functions (HOFs), including how to create your own HOFs. +The topics that are covered include anonymous functions, partial functions, function variables, and higher-order functions (HOFs), including how to create your own HOFs. diff --git a/_overviews/scala3-book/fun-partial-functions.md b/_overviews/scala3-book/fun-partial-functions.md new file mode 100644 index 000000000..4a6c52d5f --- /dev/null +++ b/_overviews/scala3-book/fun-partial-functions.md @@ -0,0 +1,82 @@ +--- +title: Partial Functions +type: section +description: This page shows how to use function variables in Scala. +languages: [ru, zh-cn] +num: 31 +previous-page: fun-function-variables +next-page: fun-eta-expansion +--- + +A partial function is a function that may not be defined for all values of its argument type. In Scala, partial functions +are unary functions implementing the `PartialFunction[A, B]` trait, where `A` is the argument type and `B` the result type. + +To define a partial function, use a `case` identical to those used in `match` expressions: + +{% tabs fun-partial-1 %} +{% tab 'Scala 2 and 3' %} +```scala +val doubledOdds: PartialFunction[Int, Int] = { + case i if i % 2 == 1 => i * 2 +} +``` +{% endtab %} +{% endtabs %} + +To check if a partial function is for an argument, use the `isDefinedAt` method: + +{% tabs fun-partial-2 %} +{% tab 'Scala 2 and 3' %} +```scala +doubledOdds.isDefinedAt(3) // true +doubledOdds.isDefinedAt(4) // false +``` +{% endtab %} +{% endtabs %} + +Trying to apply a partial function to an argument not belonging to its domain results in `MatchError`: + +{% tabs fun-partial-3 %} +{% tab 'Scala 2 and 3' %} +```scala +doubledOdds(4) // Exception in thread "main" scala.MatchError: 4 +``` +{% endtab %} +{% endtabs %} + +### Using partial functions + +A partial function can be passed as an argument to a method: + +{% tabs fun-partial-4 %} +{% tab 'Scala 2 and 3' %} +```scala +val res = List(1, 2, 3).collect({ case i if i % 2 == 1 => i * 2 }) // List(2, 6) +``` +{% endtab %} +{% endtabs %} + +You can define a default value for arguments not in domain with `applyOrElse`: + +{% tabs fun-partial-5 %} +{% tab 'Scala 2 and 3' %} +```scala +doubledOdds.applyOrElse(4, _ + 1) // 5 +``` +{% endtab %} +{% endtabs %} + +Two partial function can be composed with `orElse`, the second function will be applied for arguments where the first +one is not defined: + +{% tabs fun-partial-6 %} +{% tab 'Scala 2 and 3' %} +```scala +val incrementedEvens: PartialFunction[Int, Int] = { + case i if i % 2 == 0 => i + 1 +} + +val res2 = List(1, 2, 3).collect(doubledOdds.orElse(incrementedEvens)) // List(2, 3, 6) +``` +{% endtab %} +{% endtabs %} \ No newline at end of file diff --git a/_overviews/scala3-book/fun-summary.md b/_overviews/scala3-book/fun-summary.md index ffd7bb7bd..50eb480c2 100644 --- a/_overviews/scala3-book/fun-summary.md +++ b/_overviews/scala3-book/fun-summary.md @@ -3,7 +3,7 @@ title: Summary type: section description: This page provides a summary of the previous 'Functions' sections. languages: [ru, zh-cn] -num: 35 +num: 36 previous-page: fun-write-method-returns-function next-page: packaging-imports --- diff --git a/_overviews/scala3-book/fun-write-map-function.md b/_overviews/scala3-book/fun-write-map-function.md index 9799c97d5..85fd13b24 100644 --- a/_overviews/scala3-book/fun-write-map-function.md +++ b/_overviews/scala3-book/fun-write-map-function.md @@ -3,7 +3,7 @@ title: Write Your Own map Method type: section description: This page demonstrates how to create and use higher-order functions in Scala. languages: [ru, zh-cn] -num: 33 +num: 34 previous-page: fun-hofs next-page: fun-write-method-returns-function --- diff --git a/_overviews/scala3-book/fun-write-method-returns-function.md b/_overviews/scala3-book/fun-write-method-returns-function.md index 1e6fc8b30..28c05b9cf 100644 --- a/_overviews/scala3-book/fun-write-method-returns-function.md +++ b/_overviews/scala3-book/fun-write-method-returns-function.md @@ -3,7 +3,7 @@ title: Creating a Method That Returns a Function type: section description: This page demonstrates how to create and use higher-order functions in Scala. languages: [ru, zh-cn] -num: 34 +num: 35 previous-page: fun-write-map-function next-page: fun-summary --- diff --git a/_overviews/scala3-book/interacting-with-java.md b/_overviews/scala3-book/interacting-with-java.md index 7d9d9f246..00a3c5aa8 100644 --- a/_overviews/scala3-book/interacting-with-java.md +++ b/_overviews/scala3-book/interacting-with-java.md @@ -3,7 +3,7 @@ title: Interacting with Java type: chapter description: This page demonstrates how Scala code can interact with Java, and how Java code can interact with Scala code. languages: [ru, zh-cn] -num: 72 +num: 73 previous-page: tools-worksheets next-page: scala-for-java-devs --- diff --git a/_overviews/scala3-book/packaging-imports.md b/_overviews/scala3-book/packaging-imports.md index 4c5857cc5..f5665c28f 100644 --- a/_overviews/scala3-book/packaging-imports.md +++ b/_overviews/scala3-book/packaging-imports.md @@ -3,7 +3,7 @@ title: Packaging and Imports type: chapter description: A discussion of using packages and imports to organize your code, build related modules of code, control scope, and help prevent namespace collisions. languages: [ru, zh-cn] -num: 36 +num: 37 previous-page: fun-summary next-page: collections-intro --- diff --git a/_overviews/scala3-book/scala-for-java-devs.md b/_overviews/scala3-book/scala-for-java-devs.md index b80f3a427..0ba836177 100644 --- a/_overviews/scala3-book/scala-for-java-devs.md +++ b/_overviews/scala3-book/scala-for-java-devs.md @@ -3,7 +3,7 @@ title: Scala for Java Developers type: chapter description: This page is for Java developers who are interested in learning about Scala 3. languages: [zh-cn] -num: 73 +num: 74 previous-page: interacting-with-java next-page: scala-for-javascript-devs --- diff --git a/_overviews/scala3-book/scala-for-javascript-devs.md b/_overviews/scala3-book/scala-for-javascript-devs.md index b488ed8a9..26c672ae9 100644 --- a/_overviews/scala3-book/scala-for-javascript-devs.md +++ b/_overviews/scala3-book/scala-for-javascript-devs.md @@ -3,7 +3,7 @@ title: Scala for JavaScript Developers type: chapter description: This chapter provides an introduction to Scala 3 for JavaScript developers languages: [zh-cn] -num: 74 +num: 75 previous-page: scala-for-java-devs next-page: scala-for-python-devs --- diff --git a/_overviews/scala3-book/scala-for-python-devs.md b/_overviews/scala3-book/scala-for-python-devs.md index 5b0e8dec9..147f5977f 100644 --- a/_overviews/scala3-book/scala-for-python-devs.md +++ b/_overviews/scala3-book/scala-for-python-devs.md @@ -3,7 +3,7 @@ title: Scala for Python Developers type: chapter description: This page is for Python developers who are interested in learning about Scala 3. languages: [zh-cn] -num: 75 +num: 76 previous-page: scala-for-javascript-devs next-page: where-next --- diff --git a/_overviews/scala3-book/scala-tools.md b/_overviews/scala3-book/scala-tools.md index 7d6bb7a39..4469a7283 100644 --- a/_overviews/scala3-book/scala-tools.md +++ b/_overviews/scala3-book/scala-tools.md @@ -3,7 +3,7 @@ title: Scala Tools type: chapter description: This chapter looks at two commonly-used Scala tools, sbt and ScalaTest. languages: [ru, zh-cn] -num: 69 +num: 70 previous-page: concurrency next-page: tools-sbt --- diff --git a/_overviews/scala3-book/tools-sbt.md b/_overviews/scala3-book/tools-sbt.md index 3c442fc96..b68ae6632 100644 --- a/_overviews/scala3-book/tools-sbt.md +++ b/_overviews/scala3-book/tools-sbt.md @@ -3,7 +3,7 @@ title: Building and Testing Scala Projects with sbt type: section description: This section looks at a commonly-used build tool, sbt, and a testing library, ScalaTest. languages: [ru, zh-cn] -num: 70 +num: 71 previous-page: scala-tools next-page: tools-worksheets --- diff --git a/_overviews/scala3-book/tools-worksheets.md b/_overviews/scala3-book/tools-worksheets.md index a1102841a..cf14935e4 100644 --- a/_overviews/scala3-book/tools-worksheets.md +++ b/_overviews/scala3-book/tools-worksheets.md @@ -3,7 +3,7 @@ title: Worksheets type: section description: This section looks at worksheets, an alternative to Scala projects. languages: [ru, zh-cn] -num: 71 +num: 72 previous-page: tools-sbt next-page: interacting-with-java --- diff --git a/_overviews/scala3-book/types-adts-gadts.md b/_overviews/scala3-book/types-adts-gadts.md index 204519c03..356d01c16 100644 --- a/_overviews/scala3-book/types-adts-gadts.md +++ b/_overviews/scala3-book/types-adts-gadts.md @@ -3,7 +3,7 @@ title: Algebraic Data Types type: section description: This section introduces and demonstrates algebraic data types (ADTs) in Scala 3. languages: [ru, zh-cn] -num: 53 +num: 54 previous-page: types-union next-page: types-variance scala3: true diff --git a/_overviews/scala3-book/types-dependent-function.md b/_overviews/scala3-book/types-dependent-function.md index f5b5cf2d3..96f6d3621 100644 --- a/_overviews/scala3-book/types-dependent-function.md +++ b/_overviews/scala3-book/types-dependent-function.md @@ -3,7 +3,7 @@ title: Dependent Function Types type: section description: This section introduces and demonstrates dependent function types in Scala 3. languages: [ru, zh-cn] -num: 57 +num: 58 previous-page: types-structural next-page: types-others scala3: true diff --git a/_overviews/scala3-book/types-generics.md b/_overviews/scala3-book/types-generics.md index c772ea65a..84ddd4599 100644 --- a/_overviews/scala3-book/types-generics.md +++ b/_overviews/scala3-book/types-generics.md @@ -3,7 +3,7 @@ title: Generics type: section description: This section introduces and demonstrates generics in Scala 3. languages: [ru, zh-cn] -num: 50 +num: 51 previous-page: types-inferred next-page: types-intersection --- diff --git a/_overviews/scala3-book/types-inferred.md b/_overviews/scala3-book/types-inferred.md index 50cdb729e..92333b373 100644 --- a/_overviews/scala3-book/types-inferred.md +++ b/_overviews/scala3-book/types-inferred.md @@ -3,7 +3,7 @@ title: Inferred Types type: section description: This section introduces and demonstrates inferred types in Scala 3 languages: [ru, zh-cn] -num: 49 +num: 50 previous-page: types-introduction next-page: types-generics --- diff --git a/_overviews/scala3-book/types-intersection.md b/_overviews/scala3-book/types-intersection.md index 6c7e015ba..2c533ffd0 100644 --- a/_overviews/scala3-book/types-intersection.md +++ b/_overviews/scala3-book/types-intersection.md @@ -3,7 +3,7 @@ title: Intersection Types type: section description: This section introduces and demonstrates intersection types in Scala 3. languages: [ru, zh-cn] -num: 51 +num: 52 previous-page: types-generics next-page: types-union scala3: true diff --git a/_overviews/scala3-book/types-introduction.md b/_overviews/scala3-book/types-introduction.md index ef21c8b4f..77a79a084 100644 --- a/_overviews/scala3-book/types-introduction.md +++ b/_overviews/scala3-book/types-introduction.md @@ -3,7 +3,7 @@ title: Types and the Type System type: chapter description: This chapter provides an introduction to Scala 3 types and the type system. languages: [ru, zh-cn] -num: 48 +num: 49 previous-page: fp-summary next-page: types-inferred --- diff --git a/_overviews/scala3-book/types-opaque-types.md b/_overviews/scala3-book/types-opaque-types.md index 1d7483630..407674905 100644 --- a/_overviews/scala3-book/types-opaque-types.md +++ b/_overviews/scala3-book/types-opaque-types.md @@ -3,7 +3,7 @@ title: Opaque Types type: section description: This section introduces and demonstrates opaque types in Scala 3. languages: [ru, zh-cn] -num: 55 +num: 56 previous-page: types-variance next-page: types-structural scala3: true diff --git a/_overviews/scala3-book/types-others.md b/_overviews/scala3-book/types-others.md index f2db8a42b..9419073f9 100644 --- a/_overviews/scala3-book/types-others.md +++ b/_overviews/scala3-book/types-others.md @@ -3,7 +3,7 @@ title: Other Types type: section description: This section mentions other advanced types in Scala 3. languages: [ru, zh-cn] -num: 58 +num: 59 previous-page: types-dependent-function next-page: ca-contextual-abstractions-intro scala3: true diff --git a/_overviews/scala3-book/types-structural.md b/_overviews/scala3-book/types-structural.md index 8dee33f30..afa74fe34 100644 --- a/_overviews/scala3-book/types-structural.md +++ b/_overviews/scala3-book/types-structural.md @@ -3,7 +3,7 @@ title: Structural Types type: section description: This section introduces and demonstrates structural types in Scala 3. languages: [ru, zh-cn] -num: 56 +num: 57 previous-page: types-opaque-types next-page: types-dependent-function scala3: true diff --git a/_overviews/scala3-book/types-union.md b/_overviews/scala3-book/types-union.md index b61de27be..e68564660 100644 --- a/_overviews/scala3-book/types-union.md +++ b/_overviews/scala3-book/types-union.md @@ -3,7 +3,7 @@ title: Union Types type: section description: This section introduces and demonstrates union types in Scala 3. languages: [ru, zh-cn] -num: 52 +num: 53 previous-page: types-intersection next-page: types-adts-gadts scala3: true diff --git a/_overviews/scala3-book/types-variance.md b/_overviews/scala3-book/types-variance.md index deb2492e8..f2b8e3d93 100644 --- a/_overviews/scala3-book/types-variance.md +++ b/_overviews/scala3-book/types-variance.md @@ -3,7 +3,7 @@ title: Variance type: section description: This section introduces and demonstrates variance in Scala 3. languages: [ru, zh-cn] -num: 54 +num: 55 previous-page: types-adts-gadts next-page: types-opaque-types --- diff --git a/_overviews/scala3-book/where-next.md b/_overviews/scala3-book/where-next.md index 05bdad479..8eed7a163 100644 --- a/_overviews/scala3-book/where-next.md +++ b/_overviews/scala3-book/where-next.md @@ -3,7 +3,7 @@ title: Where To Go Next type: chapter description: Where to go next after reading the Scala Book languages: [zh-cn] -num: 76 +num: 77 previous-page: scala-for-python-devs next-page: --- From 38ac7a3f89839aedf44d27e2336f6689d140e623 Mon Sep 17 00:00:00 2001 From: gkepka Date: Tue, 10 Dec 2024 14:17:14 +0100 Subject: [PATCH 2/4] Add match following a period example and remove languages from Partial Functions page --- _overviews/scala3-book/control-structures.md | 18 +++++++++++++++--- .../scala3-book/fun-partial-functions.md | 1 - 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/_overviews/scala3-book/control-structures.md b/_overviews/scala3-book/control-structures.md index a6b2ad6ad..7dfa5c3de 100644 --- a/_overviews/scala3-book/control-structures.md +++ b/_overviews/scala3-book/control-structures.md @@ -1040,6 +1040,18 @@ match {% endtab %} {% endtabs %} +The `match` expression can also follow a period, which simplifies matching on results returned by chained method calls: + +{% tabs 'control-structures-33' %} +{% tab 'Scala 3 Only' %} +```scala +List(1, 2, 3).map(_ * 2).headOption.match + case Some(value) => println(s"The head is: $value") + case None => println("The list is empty") +``` +{% endtab %} +{% endtabs %} + ## try/catch/finally Like Java, Scala has a `try`/`catch`/`finally` construct to let you catch and manage exceptions. @@ -1047,8 +1059,8 @@ For consistency, Scala uses the same syntax that `match` expressions use and sup In the following example, `openAndReadAFile` is a method that does what its name implies: it opens a file and reads the text in it, assigning the result to the mutable variable `text`: -{% tabs control-structures-33 class=tabs-scala-version %} -{% tab 'Scala 2' for=control-structures-33 %} +{% tabs control-structures-34 class=tabs-scala-version %} +{% tab 'Scala 2' for=control-structures-34 %} ```scala var text = "" try { @@ -1062,7 +1074,7 @@ try { } ``` {% endtab %} -{% tab 'Scala 3' for=control-structures-33 %} +{% tab 'Scala 3' for=control-structures-34 %} ```scala var text = "" try diff --git a/_overviews/scala3-book/fun-partial-functions.md b/_overviews/scala3-book/fun-partial-functions.md index 4a6c52d5f..55ed352b2 100644 --- a/_overviews/scala3-book/fun-partial-functions.md +++ b/_overviews/scala3-book/fun-partial-functions.md @@ -2,7 +2,6 @@ title: Partial Functions type: section description: This page shows how to use function variables in Scala. -languages: [ru, zh-cn] num: 31 previous-page: fun-function-variables next-page: fun-eta-expansion From f05349a28826efbd70c196ae78123cb8df6b7f45 Mon Sep 17 00:00:00 2001 From: gkepka Date: Tue, 10 Dec 2024 14:20:33 +0100 Subject: [PATCH 3/4] Apply suggestions from review --- _overviews/scala3-book/control-structures.md | 2 +- _overviews/scala3-book/fun-partial-functions.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/_overviews/scala3-book/control-structures.md b/_overviews/scala3-book/control-structures.md index 7dfa5c3de..91cf66133 100644 --- a/_overviews/scala3-book/control-structures.md +++ b/_overviews/scala3-book/control-structures.md @@ -992,7 +992,7 @@ def pattern(x: Matchable): String = x match {% endtab %} {% endtabs %} -You can also write the code on the right side of the `=>` on multiple lines if you think is easier to read. Here’s one example: +You can also write the code on the right side of the `=>` on multiple lines if you think it is easier to read. Here is one example: {% tabs control-structures-31 class=tabs-scala-version %} {% tab 'Scala 2' for=control-structures-31 %} diff --git a/_overviews/scala3-book/fun-partial-functions.md b/_overviews/scala3-book/fun-partial-functions.md index 55ed352b2..fe8aaa50e 100644 --- a/_overviews/scala3-book/fun-partial-functions.md +++ b/_overviews/scala3-book/fun-partial-functions.md @@ -1,7 +1,7 @@ --- title: Partial Functions type: section -description: This page shows how to use function variables in Scala. +description: This page shows how to use partial functions in Scala. num: 31 previous-page: fun-function-variables next-page: fun-eta-expansion @@ -22,7 +22,7 @@ val doubledOdds: PartialFunction[Int, Int] = { {% endtab %} {% endtabs %} -To check if a partial function is for an argument, use the `isDefinedAt` method: +To check if a partial function is defined for an argument, use the `isDefinedAt` method: {% tabs fun-partial-2 %} {% tab 'Scala 2 and 3' %} From e80347979cf4b34580a813e7f40e233aa01cf2ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20K=C4=99pka?= <44374670+gkepka@users.noreply.github.com> Date: Tue, 10 Dec 2024 14:28:37 +0100 Subject: [PATCH 4/4] Update _overviews/scala3-book/control-structures.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Doeraene --- _overviews/scala3-book/control-structures.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/_overviews/scala3-book/control-structures.md b/_overviews/scala3-book/control-structures.md index 91cf66133..9d44db59c 100644 --- a/_overviews/scala3-book/control-structures.md +++ b/_overviews/scala3-book/control-structures.md @@ -1045,9 +1045,12 @@ The `match` expression can also follow a period, which simplifies matching on re {% tabs 'control-structures-33' %} {% tab 'Scala 3 Only' %} ```scala -List(1, 2, 3).map(_ * 2).headOption.match - case Some(value) => println(s"The head is: $value") - case None => println("The list is empty") +List(1, 2, 3) + .map(_ * 2) + .headOption + .match + case Some(value) => println(s"The head is: $value") + case None => println("The list is empty") ``` {% endtab %} {% endtabs %}