Skip to content

Commit dd4da87

Browse files
authored
Merge pull request #2659 from alvinj/merge-type-class-lessons
undefined
2 parents 7d5d132 + ce66adb commit dd4da87

19 files changed

+39
-84
lines changed

_overviews/scala3-book/ca-context-bounds.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type: section
44
description: This page demonstrates Context Bounds in Scala 3.
55
languages: [zh-cn]
66
num: 61
7-
previous-page: types-type-classes
7+
previous-page: ca-given-using-clauses
88
next-page: ca-given-imports
99
---
1010

_overviews/scala3-book/ca-contextual-abstractions-intro.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: This chapter provides an introduction to the Scala 3 concept of Con
55
languages: [zh-cn]
66
num: 58
77
previous-page: types-others
8-
next-page: ca-given-using-clauses
8+
next-page: ca-extension-methods
99
---
1010

1111

@@ -37,6 +37,10 @@ While these concepts were gradually “discovered” in Scala 2, they’re now w
3737
The design of Scala 3 focuses on **intent** rather than **mechanism**.
3838
Instead of offering one very powerful feature of implicits, Scala 3 offers several use-case oriented features:
3939

40+
- **Retroactively extending classes**.
41+
In Scala 2, extension methods had to be encoded using implicit conversions or implicit classes.
42+
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.
43+
4044
- **Abstracting over contextual information**.
4145
[Using clauses][givens] allow programmers to abstract over information that is available in the calling context and should be passed implicitly.
4246
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
4549
[Given instances][type-classes] allow programmers to define the _canonical value_ of a certain type.
4650
This makes programming with type-classes more straightforward without leaking implementation details.
4751

48-
- **Retroactively extending classes**.
49-
In Scala 2, extension methods had to be encoded using implicit conversions or implicit classes.
50-
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.
51-
5252
- **Viewing one type as another**.
5353
Implicit conversion have been [redesigned][implicit-conversions] from the ground up as instances of a type-class `Conversion`.
5454

_overviews/scala3-book/ca-extension-methods.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ title: Extension Methods
33
type: section
44
description: This page demonstrates how Extension Methods work in Scala 3.
55
languages: [zh-cn]
6-
num: 63
7-
previous-page: ca-given-imports
8-
next-page: ca-type-classes
6+
num: 59
7+
previous-page: ca-contextual-abstractions-intro
8+
next-page: ca-given-using-clauses
99
---
1010

1111

_overviews/scala3-book/ca-given-imports.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: This page demonstrates how 'given' import statements work in Scala
55
languages: [zh-cn]
66
num: 62
77
previous-page: ca-context-bounds
8-
next-page: ca-extension-methods
8+
next-page: ca-type-classes
99
---
1010

1111

_overviews/scala3-book/ca-given-using-clauses.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ title: Given Instances and Using Clauses
33
type: section
44
description: This page demonstrates how to use 'given' instances and 'using' clauses in Scala 3.
55
languages: [zh-cn]
6-
num: 59
7-
previous-page: ca-contextual-abstractions-intro
8-
next-page: types-type-classes
6+
num: 60
7+
previous-page: ca-extension-methods
8+
next-page: ca-context-bounds
99
---
1010

1111

_overviews/scala3-book/ca-implicit-conversions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Implicit Conversions
33
type: section
44
description: This page demonstrates how to implement Implicit Conversions in Scala 3.
55
languages: [zh-cn]
6-
num: 66
6+
num: 65
77
previous-page: ca-multiversal-equality
88
next-page: ca-summary
99
---

_overviews/scala3-book/ca-multiversal-equality.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Multiversal Equality
33
type: section
44
description: This page demonstrates how to implement Multiversal Equality in Scala 3.
55
languages: [zh-cn]
6-
num: 65
6+
num: 64
77
previous-page: ca-type-classes
88
next-page: ca-implicit-conversions
99
---

_overviews/scala3-book/ca-summary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Summary
33
type: section
44
description: This page provides a summary of the Contextual Abstractions lessons.
55
languages: [zh-cn]
6-
num: 67
6+
num: 66
77
previous-page: ca-implicit-conversions
88
next-page: concurrency
99
---

_overviews/scala3-book/ca-type-classes.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
---
2-
title: Implementing Type Classes
2+
title: Type Classes
33
type: section
44
description: This page demonstrates how to create and use type classes in Scala 3.
55
languages: [zh-cn]
6-
num: 64
7-
previous-page: ca-extension-methods
6+
num: 63
7+
previous-page: ca-given-imports
88
next-page: ca-multiversal-equality
99
---
1010

1111

1212
A _type class_ is an abstract, parameterized type that lets you add new behavior to any closed data type without using sub-typing.
13-
This is useful in multiple use-cases, for example:
13+
If you are coming from Java, you can think of type classes as something like [`java.util.Comparator[T]`][comparator].
14+
15+
> The paper [“Type Classes as Objects and Implicits”][typeclasses-paper] (2010) by Oliveira et al. discusses the basic ideas behind type classes in Scala.
16+
> Even though the paper uses an older version of Scala, the ideas still hold to the current day.
17+
18+
A type class is useful in multiple use-cases, for example:
1419

1520
- Expressing how a type you don’t own---from the standard library or a third-party library---conforms to such behavior
1621
- Expressing such a behavior for multiple types without involving sub-typing relationships between those types
@@ -108,4 +113,7 @@ trait HasLegs[A]:
108113
For a real-world example of how type classes are used in Scala 3, see the `CanEqual` discussion in the [Multiversal Equality section][multiversal].
109114

110115

116+
[typeclasses-paper]: https://infoscience.epfl.ch/record/150280/files/TypeClasses.pdf
117+
[typeclasses-chapter]: {% link _overviews/scala3-book/ca-type-classes.md %}
118+
[comparator]: https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
111119
[multiversal]: {% link _overviews/scala3-book/ca-multiversal-equality.md %}

_overviews/scala3-book/concurrency.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Concurrency
33
type: chapter
44
description: This page discusses how Scala concurrency works, with an emphasis on Scala Futures.
55
languages: [zh-cn]
6-
num: 68
6+
num: 67
77
previous-page: ca-summary
88
next-page: scala-tools
99
---

0 commit comments

Comments
 (0)