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
The migration to the new scheme is complicated, in particular since the [kind projector](https://github.com/typelevel/kind-projector)
27
27
compiler plugin still uses the reverse convention, with `?` meaning parameter placeholder instead of wildcard. Fortunately, kind projector has added `*` as an alternative syntax for `?`.
Copy file name to clipboardExpand all lines: docs/_docs/reference/contextual/context-functions.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,7 @@ For example, continuing with the previous definitions,
48
48
g((ctx: ExecutionContext) ?=> f(3)(using ctx)) // is left as it is
49
49
```
50
50
51
-
###Example: Builder Pattern
51
+
## Example: Builder Pattern
52
52
53
53
Context function types have considerable expressive power. For
54
54
instance, here is how they can support the "builder pattern", where
@@ -112,7 +112,7 @@ With that setup, the table construction code above compiles and expands to:
112
112
}(using $t)
113
113
}
114
114
```
115
-
###Example: Postconditions
115
+
## Example: Postconditions
116
116
117
117
As a larger example, here is a way to define constructs for checking arbitrary postconditions using an extension method `ensuring` so that the checked result can be referred to simply by `result`. The example combines opaque type aliases, context function types, and extension methods to provide a zero-overhead abstraction.
118
118
@@ -146,7 +146,7 @@ val s =
146
146
assert(result ==6)
147
147
result
148
148
```
149
-
###Reference
149
+
## Reference
150
150
151
151
For more information, see the [blog article](https://www.scala-lang.org/blog/2016/12/07/implicit-function-types.html),
152
152
(which uses a different syntax that has been superseded).
Scala's implicits are its most distinguished feature. They are _the_ fundamental way to abstract over context. They represent a unified paradigm with a great variety of use cases, among them: implementing type classes, establishing context, dependency injection, expressing capabilities, computing new types and proving relationships between them.
10
10
@@ -46,7 +46,7 @@ Historically, many of these shortcomings come from the way implicits were gradua
46
46
47
47
Existing Scala programmers by and large have gotten used to the status quo and see little need for change. But for newcomers this status quo presents a big hurdle. I believe if we want to overcome that hurdle, we should take a step back and allow ourselves to consider a radically new design.
48
48
49
-
###The New Design
49
+
## The New Design
50
50
51
51
The following pages introduce a redesign of contextual abstractions in Scala. They introduce four fundamental changes:
We say that `Tree` is the _deriving type_ and that the `Eq`, `Ordering` and `Show` instances are _derived instances_.
28
28
29
-
###Types supporting `derives` clauses
29
+
## Types supporting `derives` clauses
30
30
31
31
All data types can have a `derives` clause. This document focuses primarily on data types which also have a given instance
32
32
of the `Mirror` type class available.
@@ -158,7 +158,7 @@ Note the following properties of `Mirror` types,
158
158
+ The methods `ordinal` and `fromProduct` are defined in terms of `MirroredMonoType` which is the type of kind-`*`
159
159
which is obtained from `MirroredType` by wildcarding its type parameters.
160
160
161
-
###Type classes supporting automatic deriving
161
+
## Type classes supporting automatic deriving
162
162
163
163
A trait or class can appear in a `derives` clause if its companion object defines a method named `derived`. The
164
164
signature and implementation of a `derived` method for a type class `TC[_]` are arbitrary but it is typically of the
@@ -186,7 +186,7 @@ authors would normally implement a `derived` method in this way, however this wa
186
186
authors of the higher level derivation libraries that we expect typical type class authors will use (for a fully
187
187
worked out example of such a library, see [Shapeless 3](https://github.com/milessabin/shapeless/tree/shapeless-3)).
188
188
189
-
####How to write a type class `derived` method using low level mechanisms
189
+
### How to write a type class `derived` method using low level mechanisms
190
190
191
191
The low-level method we will use to implement a type class `derived` method in this example exploits three new
192
192
type-level constructs in Scala 3: inline methods, inline matches, and implicit searches via `summonInline` or `summonFrom`. Given this definition of the
@@ -360,7 +360,7 @@ The framework described here enables all three of these approaches without manda
360
360
For a brief discussion on how to use macros to write a type class `derived`
361
361
method please read more at [How to write a type class `derived` method using macros](./derivation-macro.md).
362
362
363
-
###Deriving instances elsewhere
363
+
## Deriving instances elsewhere
364
364
365
365
Sometimes one would like to derive a type class instance for an ADT after the ADT is defined, without being able to
366
366
change the code of the ADT itself. To do this, simply define an instance using the `derived` method of the type class
@@ -374,7 +374,7 @@ Assuming the `Ordering.derived` method has a context parameter of type `Mirror[T
374
374
compiler generated `Mirror` instance for `Option` and the derivation of the instance will be expanded on the right
375
375
hand side of this definition in the same way as an instance defined in ADT companion objects.
376
376
377
-
###Syntax
377
+
## Syntax
378
378
379
379
```
380
380
Template ::= InheritClauses [TemplateBody]
@@ -397,7 +397,7 @@ It is equivalent to the old form
397
397
classAextendsBwithC { ... }
398
398
```
399
399
400
-
###Discussion
400
+
## Discussion
401
401
402
402
This type class derivation framework is intentionally very small and low-level. There are essentially two pieces of
403
403
infrastructure in compiler-generated `Mirror` instances,
Copy file name to clipboardExpand all lines: docs/_docs/reference/contextual/extension-methods.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ val circle = Circle(0, 0, 1)
20
20
circle.circumference
21
21
```
22
22
23
-
###Translation of Extension Methods
23
+
## Translation of Extension Methods
24
24
25
25
An extension method translates to a specially labelled method that takes the leading parameter section as its first argument list. The label, expressed
26
26
as `<extension>` here, is compiler-internal. So, the definition of `circumference` above translates to the following method, and can also be invoked as such:
@@ -31,7 +31,7 @@ as `<extension>` here, is compiler-internal. So, the definition of `circumferenc
Copy file name to clipboardExpand all lines: docs/_docs/reference/contextual/given-imports.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ There are two main benefits arising from these rules:
40
40
can be anonymous, so the usual recourse of using named imports is not
41
41
practical.
42
42
43
-
###Importing By Type
43
+
## Importing By Type
44
44
45
45
Since givens can be anonymous it is not always practical to import them by their name, and wildcard imports are typically used instead. By-type imports provide a more specific alternative to wildcard imports, which makes it clearer what is imported. Example:
46
46
@@ -82,7 +82,7 @@ import Instances.{im, given Ordering[?]}
82
82
83
83
would import `im`, `intOrd`, and `listOrd` but leave out `ec`.
84
84
85
-
###Migration
85
+
## Migration
86
86
87
87
The rules for imports stated above have the consequence that a library
88
88
would have to migrate in lockstep with all its users from old style implicits and
@@ -101,7 +101,7 @@ These rules mean that library users can use `given` selectors to access old-styl
101
101
and will be gently nudged and then forced to do so in later versions. Libraries can then switch to
102
102
given instances once their user base has migrated.
0 commit comments