Skip to content

Commit 8159550

Browse files
committed
All paragraphs are now separate lines to simplify PRs
1 parent dceb41b commit 8159550

File tree

70 files changed

+1344
-611
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1344
-611
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ next-page: ca-given-imports
1414
{% endcomment %}
1515

1616

17-
In many situations the name of a *context parameter* doesn’t have to be mentioned explicitly, since it’s only used in synthesized arguments for other context parameters. In that case you don’t have to define a parameter name, and can just provide the parameter type.
17+
In many situations the name of a *context parameter* doesn’t have to be mentioned explicitly, since it’s only used in synthesized arguments for other context parameters.
18+
In that case you don’t have to define a parameter name, and can just provide the parameter type.
1819

1920

2021
## Background

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,53 @@ next-page: ca-given-using-clauses
1010

1111
## Background
1212

13-
Implicits in Scala 2 were a major distinguishing design feature. They are *the* fundamental way to abstract over context. They represent a unified paradigm with a great variety of use cases, among them:
13+
Implicits in Scala 2 were a major distinguishing design feature.
14+
They are *the* fundamental way to abstract over context.
15+
They represent a unified paradigm with a great variety of use cases, among them:
1416

1517
- Implementing type classes
1618
- Establishing context
1719
- Dependency injection
1820
- Expressing capabilities
1921
- Computing new types, and proving relationships between them
2022

21-
Since then, other languages have followed suit, e.g., Rust’s traits or Swift’s protocol extensions. Design proposals are also on the table for Kotlin as compile time dependency resolution, for C# as Shapes and Extensions or for F# as Traits. Implicits are also a common feature of theorem provers such as Coq or Agda.
23+
Since then, other languages have followed suit, e.g., Rust’s traits or Swift’s protocol extensions.
24+
Design proposals are also on the table for Kotlin as compile time dependency resolution, for C# as Shapes and Extensions or for F# as Traits.
25+
Implicits are also a common feature of theorem provers such as Coq or Agda.
2226

23-
Even though these designs use different terminology, they’re all variants of the core idea of *term inference*: Given a type, the compiler synthesizes a “canonical” term that has that type.
27+
Even though these designs use different terminology, they’re all variants of the core idea of *term inference*:
28+
Given a type, the compiler synthesizes a “canonical” term that has that type.
2429

2530

2631
## Redesign
2732

28-
Scala 3 includes a redesign of contextual abstractions in Scala. While these concepts were gradually “discovered” in Scala 2, they’re now well known and understood, and the redesign takes advantage of that knowledge.
33+
Scala 3 includes a redesign of contextual abstractions in Scala.
34+
While these concepts were gradually “discovered” in Scala 2, they’re now well known and understood, and the redesign takes advantage of that knowledge.
2935

3036
The design of Scala 3 focuses on **intent** rather than **mechanism**.
3137
Instead of offering one very powerful feature of implicits, Scala 3 offers several use-case oriented features:
3238

33-
- **Abtracting 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.
39+
- **Abtracting over contextual information**.
40+
[Using clauses][givens] allow programmers to abstract over information that is available in the calling context and should be passed implicitly.
41+
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.
3442

35-
- **Providing Type-class instances**. [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.
43+
- **Providing Type-class instances**.
44+
[Given instances][type-classes] allow programmers to define the _canonical value_ of a certain type.
45+
This makes programming with type-classes more straightforward without leaking implementation details.
3646

37-
- **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.
47+
- **Retroactively extending classes**.
48+
In Scala 2, extension methods had to be encoded using implicit conversions or implicit classes.
49+
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.
3850

39-
- **Viewing one type as another**. Implicit conversion have been [redesigned][implicit-conversions] from the ground up as instances of a type-class `Conversion`.
51+
- **Viewing one type as another**.
52+
Implicit conversion have been [redesigned][implicit-conversions] from the ground up as instances of a type-class `Conversion`.
4053

41-
- **Higher-order contextual abstractions**. The _all-new_ feature of [context functions][contextual-functions] makes contextual abstractions a first-class citizen. They are an important tool for library authors and allow to express concise domain specific languages.
54+
- **Higher-order contextual abstractions**.
55+
The _all-new_ feature of [context functions][contextual-functions] makes contextual abstractions a first-class citizen.
56+
They are an important tool for library authors and allow to express concise domain specific languages.
4257

43-
- **Actionable feedback from the compiler**. In case an implicit parameter can not be resolved by the compiler, it now provides you [import suggestions](https://www.scala-lang.org/blog/2020/05/05/scala-3-import-suggestions.html) that may fix the problem.
58+
- **Actionable feedback from the compiler**.
59+
In case an implicit parameter can not be resolved by the compiler, it now provides you [import suggestions](https://www.scala-lang.org/blog/2020/05/05/scala-3-import-suggestions.html) that may fix the problem.
4460

4561

4662
## Benefits

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ next-page: ca-type-classes
88
---
99

1010

11-
Extension methods let you add methods to a type after the type is defined, i.e., they let you add new methods to closed classes. For example, imagine that someone else has created a `Circle` class:
11+
Extension methods let you add methods to a type after the type is defined, i.e., they let you add new methods to closed classes.
12+
For example, imagine that someone else has created a `Circle` class:
1213

1314
```scala
1415
case class Circle(x: Double, y: Double, radius: Double)
1516
```
1617

17-
Now imagine that you need a `circumference` method, but you can’t modify their source code. Before the concept of term inference was introduced into programming languages, the only thing you could do was write a method in a separate class or object like this:
18+
Now imagine that you need a `circumference` method, but you can’t modify their source code.
19+
Before the concept of term inference was introduced into programming languages, the only thing you could do was write a method in a separate class or object like this:
1820

1921
```scala
2022
object CircleHelpers:
@@ -49,7 +51,8 @@ aCircle.circumference
4951

5052
## Discussion
5153

52-
The `extension` keyword declares that you’re about to define one or more extension methods on the type that’s put in parentheses. To define multiple extension methods on a type, use this syntax:
54+
The `extension` keyword declares that you’re about to define one or more extension methods on the type that’s put in parentheses.
55+
To define multiple extension methods on a type, use this syntax:
5356

5457
```scala
5558
extension (c: Circle)

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ next-page: ca-extension-methods
88
---
99

1010

11-
To make it more clear where givens in the current scope are coming from, a special form of the `import` statement is used to import `given` instances. The basic form is shown in this example:
11+
To make it more clear where givens in the current scope are coming from, a special form of the `import` statement is used to import `given` instances.
12+
The basic form is shown in this example:
1213

1314
```scala
1415
object A:
@@ -21,7 +22,9 @@ object B:
2122
import A.given // import the given instance
2223
```
2324

24-
In this code the `import A._` clause of object `B` imports all members of `A` *except* the `given` instance, `tc`. Conversely, the second import, `import A.given`, imports *only* that `given` instance. The two `import` clauses can also be merged into one:
25+
In this code the `import A._` clause of object `B` imports all members of `A` *except* the `given` instance, `tc`.
26+
Conversely, the second import, `import A.given`, imports *only* that `given` instance.
27+
The two `import` clauses can also be merged into one:
2528

2629
```scala
2730
object B:
@@ -35,8 +38,10 @@ The wildcard selector `_` brings all definitions other than givens or extensions
3538

3639
These rules have two main benefits:
3740

38-
- It’s more clear where givens in the current scope are coming from. In particular, it’s not possible to hide imported givens in a long list of other wildcard imports.
39-
- It enables importing all givens without importing anything else. This is important because givens can be anonymous, so the usual use of named imports is not practical.
41+
- It’s more clear where givens in the current scope are coming from.
42+
In particular, it’s not possible to hide imported givens in a long list of other wildcard imports.
43+
- It enables importing all givens without importing anything else.
44+
This is important because givens can be anonymous, so the usual use of named imports is not practical.
4045

4146
More examples of the “import given” syntax are shown in the [Packaging and Imports chapter][imports].
4247

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ Scala 3 offers two important feature for contextual abstraction:
1313
- **Given Instances** let you define terms that can be used by the Scala compiler to fill in the missing arguments.
1414

1515
## Using Clauses
16-
When designing a system, often context information like _configuration_ or settings need to be provided to the different components of your system. One common way to achieve this is by passing the configuration as additional argument to your methods.
16+
When designing a system, often context information like _configuration_ or settings need to be provided to the different components of your system.
17+
One common way to achieve this is by passing the configuration as additional argument to your methods.
1718

1819
In the following example, we define a case class `Config` to model some website configuration and pass it around in the different methods.
1920
```scala
@@ -27,7 +28,8 @@ def renderWidget(items: List[String], c: Config): String = ???
2728
val config = Config(8080, "docs.scala-lang.org")
2829
renderWebsite("/home")(config)
2930
```
30-
Let us assume that the configuration does not change throughout most of our code base. Passing `c` to each and every method call (like `renderWidget`) becomes very tedious and makes our program more difficult to read, since we need to ignore the `c` argument.
31+
Let us assume that the configuration does not change throughout most of our code base.
32+
Passing `c` to each and every method call (like `renderWidget`) becomes very tedious and makes our program more difficult to read, since we need to ignore the `c` argument.
3133

3234
#### Using `using` to mark parameters as contextual
3335
In Scala 3, we can mark some of the parameters of our methods as _contextual_.
@@ -39,9 +41,11 @@ def renderWebsite(path: String)(using c: Config): String =
3941

4042
def renderWidget(items: List[String])(using c: Config): String = ???
4143
```
42-
By starting a parameter section with the keyword `using`, we tell the Scala compiler that at the callsite it should automatically find an argument with the correct type. The Scala compiler thus performs **term inference**.
44+
By starting a parameter section with the keyword `using`, we tell the Scala compiler that at the callsite it should automatically find an argument with the correct type.
45+
The Scala compiler thus performs **term inference**.
4346

44-
In our call to `renderWidget(List("cart"))` the Scala compiler will see that there is a term of type `Config` in scope (the `c`) and automatically provide it to `renderWidget`. So the program is equivalent to the one above.
47+
In our call to `renderWidget(List("cart"))` the Scala compiler will see that there is a term of type `Config` in scope (the `c`) and automatically provide it to `renderWidget`.
48+
So the program is equivalent to the one above.
4549

4650
In fact, since we do not need to refer to `c` in our implementation of `renderWebsite` anymore, we can even omit it in the signature:
4751

@@ -53,7 +57,8 @@ def renderWebsite(path: String)(using Config): String =
5357
```
5458

5559
#### Explicitly providing contextual arguments
56-
We have seen how to _abstract_ over contextual parameters and that the Scala compiler can provide arguments automatically for us. But how can we specify which configuration to use for our call to `renderWebsite`?
60+
We have seen how to _abstract_ over contextual parameters and that the Scala compiler can provide arguments automatically for us.
61+
But how can we specify which configuration to use for our call to `renderWebsite`?
5762

5863
Like we specified our parameter section with `using`, we can also explicitly provide contextual arguments with `using:`
5964

@@ -65,7 +70,8 @@ Explicitly providing contextual parameters can be useful if we have multiple dif
6570
For all other cases, as we will see in the next Section, there is also another way to bring contextual values into scope.
6671

6772
## Given Instances
68-
We have seen that we can explicitly pass arguments as contextual parameters by marking the argument section of the _call_ with `using`. However, if there is _a single canonical value_ for a particular type, there is another preferred way to make it available to the Scala compiler: by marking it as `given`.
73+
We have seen that we can explicitly pass arguments as contextual parameters by marking the argument section of the _call_ with `using`.
74+
However, if there is _a single canonical value_ for a particular type, there is another preferred way to make it available to the Scala compiler: by marking it as `given`.
6975

7076
```scala
7177
val config = Config(8080, "docs.scala-lang.org")

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ next-page: ca-summary
88
---
99

1010

11-
Implicit conversions are defined by `given` instances of the _scala.Conversion_ class. For example, not accounting for possible conversion errors, this code defines an an implicit conversion from `String` to `Int`:
11+
Implicit conversions are defined by `given` instances of the _scala.Conversion_ class.
12+
For example, not accounting for possible conversion errors, this code defines an an implicit conversion from `String` to `Int`:
1213

1314
```scala
1415
given Conversion[String, Int] with
@@ -35,7 +36,8 @@ plus1("1")
3536

3637
## Discussion
3738

38-
The Predef package contains “auto-boxing” conversions that map primitive number types to subclasses of _java.lang.Number_. For instance, the conversion from `Int` to _java.lang.Integer_ can be defined as follows:
39+
The Predef package contains “auto-boxing” conversions that map primitive number types to subclasses of _java.lang.Number_.
40+
For instance, the conversion from `Int` to _java.lang.Integer_ can be defined as follows:
3941

4042
```scala
4143
given int2Integer: Conversion[Int, java.lang.Integer] =

0 commit comments

Comments
 (0)