You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _overviews/scala3-book/ca-context-bounds.md
+2-1Lines changed: 2 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,8 @@ next-page: ca-given-imports
14
14
{% endcomment %}
15
15
16
16
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.
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:
14
16
15
17
- Implementing type classes
16
18
- Establishing context
17
19
- Dependency injection
18
20
- Expressing capabilities
19
21
- Computing new types, and proving relationships between them
20
22
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.
22
26
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.
24
29
25
30
26
31
## Redesign
27
32
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.
29
35
30
36
The design of Scala 3 focuses on **intent** rather than **mechanism**.
31
37
Instead of offering one very powerful feature of implicits, Scala 3 offers several use-case oriented features:
32
38
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.
34
42
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.
36
46
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.
38
50
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`.
40
53
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.
42
57
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.
Copy file name to clipboardExpand all lines: _overviews/scala3-book/ca-extension-methods.md
+6-3Lines changed: 6 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -8,13 +8,15 @@ next-page: ca-type-classes
8
8
---
9
9
10
10
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:
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:
18
20
19
21
```scala
20
22
objectCircleHelpers:
@@ -49,7 +51,8 @@ aCircle.circumference
49
51
50
52
## Discussion
51
53
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:
Copy file name to clipboardExpand all lines: _overviews/scala3-book/ca-given-imports.md
+9-4Lines changed: 9 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,8 @@ next-page: ca-extension-methods
8
8
---
9
9
10
10
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:
12
13
13
14
```scala
14
15
objectA:
@@ -21,7 +22,9 @@ object B:
21
22
importA.given// import the given instance
22
23
```
23
24
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:
25
28
26
29
```scala
27
30
objectB:
@@ -35,8 +38,10 @@ The wildcard selector `_` brings all definitions other than givens or extensions
35
38
36
39
These rules have two main benefits:
37
40
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.
40
45
41
46
More examples of the “import given” syntax are shown in the [Packaging and Imports chapter][imports].
Copy file name to clipboardExpand all lines: _overviews/scala3-book/ca-given-using-clauses.md
+12-6Lines changed: 12 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,8 @@ Scala 3 offers two important feature for contextual abstraction:
13
13
-**Given Instances** let you define terms that can be used by the Scala compiler to fill in the missing arguments.
14
14
15
15
## 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.
17
18
18
19
In the following example, we define a case class `Config` to model some website configuration and pass it around in the different methods.
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.
31
33
32
34
#### Using `using` to mark parameters as contextual
33
35
In Scala 3, we can mark some of the parameters of our methods as _contextual_.
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**.
43
46
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.
45
49
46
50
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:
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`?
57
62
58
63
Like we specified our parameter section with `using`, we can also explicitly provide contextual arguments with `using:`
59
64
@@ -65,7 +70,8 @@ Explicitly providing contextual parameters can be useful if we have multiple dif
65
70
For all other cases, as we will see in the next Section, there is also another way to bring contextual values into scope.
66
71
67
72
## 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`.
Copy file name to clipboardExpand all lines: _overviews/scala3-book/ca-implicit-conversions.md
+4-2Lines changed: 4 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,8 @@ next-page: ca-summary
8
8
---
9
9
10
10
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`:
12
13
13
14
```scala
14
15
givenConversion[String, Int] with
@@ -35,7 +36,8 @@ plus1("1")
35
36
36
37
## Discussion
37
38
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:
0 commit comments