Skip to content

Merged the Type Class lessons and moved the Extension Methods lesson #2659

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 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-context-bounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---

Expand Down
10 changes: 5 additions & 5 deletions _overviews/scala3-book/ca-contextual-abstractions-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---


Expand Down Expand Up @@ -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.
Expand All @@ -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`.

Expand Down
6 changes: 3 additions & 3 deletions _overviews/scala3-book/ca-extension-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---


Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-given-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---


Expand Down
6 changes: 3 additions & 3 deletions _overviews/scala3-book/ca-given-using-clauses.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---


Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-implicit-conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-multiversal-equality.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
16 changes: 12 additions & 4 deletions _overviews/scala3-book/ca-type-classes.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 %}
2 changes: 1 addition & 1 deletion _overviews/scala3-book/concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/interacting-with-java.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/scala-for-java-devs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/scala-for-javascript-devs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
4 changes: 2 additions & 2 deletions _overviews/scala3-book/scala-for-python-devs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down Expand Up @@ -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 %}
</div>
2 changes: 1 addition & 1 deletion _overviews/scala3-book/scala-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/tools-sbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/tools-worksheets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
53 changes: 0 additions & 53 deletions _overviews/scala3-book/types-type-classes.md

This file was deleted.

4 changes: 2 additions & 2 deletions _overviews/scala3-book/where-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down