From ce66adbf9a028d576473099895231b973bd7c8e3 Mon Sep 17 00:00:00 2001 From: Alvin Alexander Date: Thu, 15 Dec 2022 12:17:32 -0500 Subject: [PATCH] Merged the Type Class lessons (updating related files) and moved Extension Methods lesson --- _overviews/scala3-book/ca-context-bounds.md | 2 +- .../ca-contextual-abstractions-intro.md | 10 ++-- .../scala3-book/ca-extension-methods.md | 6 +-- _overviews/scala3-book/ca-given-imports.md | 2 +- .../scala3-book/ca-given-using-clauses.md | 6 +-- .../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 | 16 ++++-- _overviews/scala3-book/concurrency.md | 2 +- .../scala3-book/interacting-with-java.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 | 4 +- _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-type-classes.md | 53 ------------------- _overviews/scala3-book/where-next.md | 4 +- 19 files changed, 39 insertions(+), 84 deletions(-) delete mode 100644 _overviews/scala3-book/types-type-classes.md diff --git a/_overviews/scala3-book/ca-context-bounds.md b/_overviews/scala3-book/ca-context-bounds.md index f1933fd6be..7d4faa3c4d 100644 --- a/_overviews/scala3-book/ca-context-bounds.md +++ b/_overviews/scala3-book/ca-context-bounds.md @@ -4,7 +4,7 @@ type: section description: This page demonstrates Context Bounds in Scala 3. languages: [zh-cn] num: 61 -previous-page: types-type-classes +previous-page: ca-given-using-clauses next-page: ca-given-imports --- diff --git a/_overviews/scala3-book/ca-contextual-abstractions-intro.md b/_overviews/scala3-book/ca-contextual-abstractions-intro.md index 9f2d0955d7..4174bcf5ee 100644 --- a/_overviews/scala3-book/ca-contextual-abstractions-intro.md +++ b/_overviews/scala3-book/ca-contextual-abstractions-intro.md @@ -5,7 +5,7 @@ description: This chapter provides an introduction to the Scala 3 concept of Con languages: [zh-cn] num: 58 previous-page: types-others -next-page: ca-given-using-clauses +next-page: ca-extension-methods --- @@ -37,6 +37,10 @@ While these concepts were gradually “discovered” in Scala 2, they’re now w The design of Scala 3 focuses on **intent** rather than **mechanism**. Instead of offering one very powerful feature of implicits, Scala 3 offers several use-case oriented features: +- **Retroactively extending classes**. + In Scala 2, extension methods had to be encoded using implicit conversions or implicit classes. + In contrast, in Scala 3 [extension methods][extension-methods] are now directly built into the language, leading to better error messages and improved type inference. + - **Abstracting over contextual information**. [Using clauses][givens] allow programmers to abstract over information that is available in the calling context and should be passed implicitly. As an improvement over Scala 2 implicits, using clauses can be specified by type, freeing function signatures from term variable names that are never explicitly referred to. @@ -45,10 +49,6 @@ Instead of offering one very powerful feature of implicits, Scala 3 offers sever [Given instances][type-classes] allow programmers to define the _canonical value_ of a certain type. This makes programming with type-classes more straightforward without leaking implementation details. -- **Retroactively extending classes**. - In Scala 2, extension methods had to be encoded using implicit conversions or implicit classes. - In contrast, in Scala 3 [extension methods][extension-methods] are now directly built into the language, leading to better error messages and improved type inference. - - **Viewing one type as another**. Implicit conversion have been [redesigned][implicit-conversions] from the ground up as instances of a type-class `Conversion`. diff --git a/_overviews/scala3-book/ca-extension-methods.md b/_overviews/scala3-book/ca-extension-methods.md index efa2348dc0..354bf8939e 100644 --- a/_overviews/scala3-book/ca-extension-methods.md +++ b/_overviews/scala3-book/ca-extension-methods.md @@ -3,9 +3,9 @@ title: Extension Methods type: section description: This page demonstrates how Extension Methods work in Scala 3. languages: [zh-cn] -num: 63 -previous-page: ca-given-imports -next-page: ca-type-classes +num: 59 +previous-page: ca-contextual-abstractions-intro +next-page: ca-given-using-clauses --- diff --git a/_overviews/scala3-book/ca-given-imports.md b/_overviews/scala3-book/ca-given-imports.md index 33b775b2eb..9b009001a9 100644 --- a/_overviews/scala3-book/ca-given-imports.md +++ b/_overviews/scala3-book/ca-given-imports.md @@ -5,7 +5,7 @@ description: This page demonstrates how 'given' import statements work in Scala languages: [zh-cn] num: 62 previous-page: ca-context-bounds -next-page: ca-extension-methods +next-page: ca-type-classes --- diff --git a/_overviews/scala3-book/ca-given-using-clauses.md b/_overviews/scala3-book/ca-given-using-clauses.md index 399a6d1d6f..d762e8719a 100644 --- a/_overviews/scala3-book/ca-given-using-clauses.md +++ b/_overviews/scala3-book/ca-given-using-clauses.md @@ -3,9 +3,9 @@ title: Given Instances and Using Clauses type: section description: This page demonstrates how to use 'given' instances and 'using' clauses in Scala 3. languages: [zh-cn] -num: 59 -previous-page: ca-contextual-abstractions-intro -next-page: types-type-classes +num: 60 +previous-page: ca-extension-methods +next-page: ca-context-bounds --- diff --git a/_overviews/scala3-book/ca-implicit-conversions.md b/_overviews/scala3-book/ca-implicit-conversions.md index baadbaa9df..6294ffd64a 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 3. languages: [zh-cn] -num: 66 +num: 65 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 6ac592d4db..dc16effb35 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: [zh-cn] -num: 65 +num: 64 previous-page: ca-type-classes next-page: ca-implicit-conversions --- diff --git a/_overviews/scala3-book/ca-summary.md b/_overviews/scala3-book/ca-summary.md index 60c1e5e686..ede541abe2 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: [zh-cn] -num: 67 +num: 66 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 1aad088767..e0e81c8d50 100644 --- a/_overviews/scala3-book/ca-type-classes.md +++ b/_overviews/scala3-book/ca-type-classes.md @@ -1,16 +1,21 @@ --- -title: Implementing Type Classes +title: Type Classes type: section description: This page demonstrates how to create and use type classes in Scala 3. languages: [zh-cn] -num: 64 -previous-page: ca-extension-methods +num: 63 +previous-page: ca-given-imports next-page: ca-multiversal-equality --- A _type class_ is an abstract, parameterized type that lets you add new behavior to any closed data type without using sub-typing. -This is useful in multiple use-cases, for example: +If you are coming from Java, you can think of type classes as something like [`java.util.Comparator[T]`][comparator]. + +> The paper [“Type Classes as Objects and Implicits”][typeclasses-paper] (2010) by Oliveira et al. discusses the basic ideas behind type classes in Scala. +> Even though the paper uses an older version of Scala, the ideas still hold to the current day. + +A type class is useful in multiple use-cases, for example: - Expressing how a type you don’t own---from the standard library or a third-party library---conforms to such behavior - Expressing such a behavior for multiple types without involving sub-typing relationships between those types @@ -108,4 +113,7 @@ trait HasLegs[A]: For a real-world example of how type classes are used in Scala 3, see the `CanEqual` discussion in the [Multiversal Equality section][multiversal]. +[typeclasses-paper]: https://infoscience.epfl.ch/record/150280/files/TypeClasses.pdf +[typeclasses-chapter]: {% link _overviews/scala3-book/ca-type-classes.md %} +[comparator]: https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html [multiversal]: {% link _overviews/scala3-book/ca-multiversal-equality.md %} diff --git a/_overviews/scala3-book/concurrency.md b/_overviews/scala3-book/concurrency.md index 173e0ff72a..ff2151bd4b 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: [zh-cn] -num: 68 +num: 67 previous-page: ca-summary next-page: scala-tools --- diff --git a/_overviews/scala3-book/interacting-with-java.md b/_overviews/scala3-book/interacting-with-java.md index 06a5a8fa62..18f5667446 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: [zh-cn] -num: 72 +num: 71 previous-page: tools-worksheets next-page: scala-for-java-devs --- diff --git a/_overviews/scala3-book/scala-for-java-devs.md b/_overviews/scala3-book/scala-for-java-devs.md index 53646a7d06..61b2d558d2 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: 72 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 ff9d4ffeb5..478a0b028d 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: 73 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 70d5d43db5..e10b43ebdb 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: 74 previous-page: scala-for-javascript-devs next-page: where-next --- @@ -1339,6 +1339,6 @@ None of this manual process is necessary in Scala. [modeling-intro]: {% link _overviews/scala3-book/domain-modeling-intro.md %} [multiversal]: {% link _overviews/scala3-book/ca-multiversal-equality.md %} [toplevel]: {% link _overviews/scala3-book/taste-toplevel-definitions.md %} -[type-classes]: {% link _overviews/scala3-book/types-type-classes.md %} +[type-classes]: {% link _overviews/scala3-book/ca-type-classes.md %} [union-types]: {% link _overviews/scala3-book/types-union.md %} diff --git a/_overviews/scala3-book/scala-tools.md b/_overviews/scala3-book/scala-tools.md index 1b6badd809..230f7b74ae 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: [zh-cn] -num: 69 +num: 68 previous-page: concurrency next-page: tools-sbt --- diff --git a/_overviews/scala3-book/tools-sbt.md b/_overviews/scala3-book/tools-sbt.md index 8735cbce68..6fd9897a29 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: [zh-cn] -num: 70 +num: 69 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 5c8dacf3e5..92f7fd66ca 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: [zh-cn] -num: 71 +num: 70 previous-page: tools-sbt next-page: interacting-with-java --- diff --git a/_overviews/scala3-book/types-type-classes.md b/_overviews/scala3-book/types-type-classes.md deleted file mode 100644 index 19c20fba89..0000000000 --- a/_overviews/scala3-book/types-type-classes.md +++ /dev/null @@ -1,53 +0,0 @@ ---- -title: Type Classes -type: section -description: This section introduces type classes in Scala 3. -languages: [zh-cn] -num: 60 -previous-page: ca-given-using-clauses -next-page: ca-context-bounds ---- - -A _type class_ is an abstract, parameterized type that lets you add new behavior to any closed data type without using sub-typing. -If you are coming from Java, you can think of type classes as something like [`java.util.Comparator[T]`][comparator]. - -> The paper [“Type Classes as Objects and Implicits”][typeclasses-paper] (2010) by Oliveira et al. discusses the basic ideas behind type classes in Scala. -> Even though the paper uses an older version of Scala, the ideas still hold to the current day. - -This style of programming is useful in multiple use-cases, for example: - -- Expressing how a type you don’t own---such as from the standard library or a third-party library---conforms to such behavior -- Adding behavior to multiple types without introducing sub-typing relationships between those types (i.e., one `extends` another) - -In Scala 3, _type classes_ are just _traits_ with one or more type parameters, like the following: -``` -trait Show[A]: - def show(a: A): String -``` -Instances of `Show` for a particular type `A` witness that we can show (i.e., produce a text representation of) an instance of type `A`. -For example, let’s look at the following `Show` instance for `Int` values: - -```scala -class ShowInt extends Show[Int]: - def show(a: Int) = s"The number is ${a}!" -``` -We can write methods that work on arbitrary types `A` _constrained_ by `Show` as follows: - -```scala -def toHtml[A](a: A)(showA: Show[A]): String = - "

" + showA.show(a) + "

" -``` -That is, `toHtml` can be called with arbitrary `A` _as long_ as you can also provide an instance of `Show[A]`. -For example, we can call it like: -```scala -toHtml(42)(ShowInt()) -// results in "

The number is 42!

" -``` - -#### Automatically passing type class instances -Since type classes are a very important way to structure software, Scala 3 offers additional features that make working with them very convenient. -We discuss these additional features (which fall into the category of *Contextual Abstractions*) in a [later chapter][typeclasses-chapter] of this book. - -[typeclasses-paper]: https://infoscience.epfl.ch/record/150280/files/TypeClasses.pdf -[typeclasses-chapter]: {% link _overviews/scala3-book/ca-type-classes.md %} -[comparator]: https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html diff --git a/_overviews/scala3-book/where-next.md b/_overviews/scala3-book/where-next.md index 43e9ec8424..41bdf27255 100644 --- a/_overviews/scala3-book/where-next.md +++ b/_overviews/scala3-book/where-next.md @@ -3,9 +3,9 @@ title: Where To Go Next type: chapter description: Where to go next after reading the Scala Book languages: [zh-cn] -num: 76 +num: 75 previous-page: scala-for-python-devs -next-page: +next-page: --- We hope you enjoyed this introduction to the Scala programming language, and we also hope we were able to share some of the beauty of the language.