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: docs/docs/reference/contextual/derivation.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
@@ -34,16 +34,16 @@ case class Some[T] extends Option[T]
34
34
caseobjectNoneextendsOption[Nothing]
35
35
```
36
36
37
-
The generated typeclass delegates are placed in the companion objects `Labelled` and `Option`, respectively.
37
+
The generated typeclass instances are placed in the companion objects `Labelled` and `Option`, respectively.
38
38
39
39
### Derivable Types
40
40
41
41
A trait or class can appear in a `derives` clause if its companion object defines a method named `derived`. The type and implementation of a `derived` method are arbitrary, but typically it has a definition like this:
42
42
```scala
43
-
defderived[T] whereMirror.Of[T] = ...
43
+
defderived[T] givenMirror.Of[T] = ...
44
44
```
45
45
That is, the `derived` method takes an implicit parameter of (some subtype of) type `Mirror` that defines the shape of the deriving type `T` and it computes the typeclass implementation according
46
-
to that shape. A `Mirror`delegate is generated automatically for
46
+
to that shape. A given `Mirror`instance is generated automatically for
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/extension-methods.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ When is an extension method applicable? There are two possibilities.
36
36
37
37
- An extension method is applicable if it is visible under a simple name, by being defined
38
38
or inherited or imported in a scope enclosing the application.
39
-
- An extension method is applicable if it is a member of some instance that is given at the point of the application.
39
+
- An extension method is applicable if it is a member of some given instance at the point of the application.
40
40
41
41
As an example, consider an extension method `longestStrings` on `String` defined in a trait `StringSeqOps`.
42
42
@@ -48,15 +48,15 @@ trait StringSeqOps {
48
48
}
49
49
}
50
50
```
51
-
We can make the extension method available by defining a given of class `StringSeqOps`, like this:
51
+
We can make the extension method available by defining a given `StringSeqOps` instance, like this:
52
52
```scala
53
53
givenops1 as StringSeqOps
54
54
```
55
55
Then
56
56
```scala
57
57
List("here", "is", "a", "list").longestStrings
58
58
```
59
-
is legal everywhere `ops1` is available as a given instance. Alternatively, we can define `longestStrings` as a member of a normal object. But then the method has to be brought into scope to be usable as an extension method.
59
+
is legal everywhere `ops1` is available. Alternatively, we can define `longestStrings` as a member of a normal object. But then the method has to be brought into scope to be usable as an extension method.
60
60
61
61
```scala
62
62
objectops2extendsStringSeqOps
@@ -76,7 +76,7 @@ and where `T` is the expected type. The following two rewritings are tried in or
76
76
from `T` to a type containing `m`. If there is more than one way of rewriting, an ambiguity error results.
77
77
78
78
So `circle.circumference` translates to `CircleOps.circumference(circle)`, provided
79
-
`circle` has type `Circle` and `CircleOps` is a given instance (i.e. it is visible at the point of call or it is defined in the companion object of `Circle`).
79
+
`circle` has type `Circle` and `CircleOps` is given (i.e. it is visible at the point of call or it is defined in the companion object of `Circle`).
Conversely, if the expected type of an expression `E` is an implicit function type
25
23
`given (T_1, ..., T_n) => U` and `E` is not already an
26
24
implicit function literal, `E` is converted to an implicit function literal by rewriting to
@@ -29,7 +27,7 @@ implicit function literal, `E` is converted to an implicit function literal by r
29
27
```
30
28
where the names `x_1`, ..., `x_n` are arbitrary. This expansion is performed
31
29
before the expression `E` is typechecked, which means that `x_1`, ..., `x_n`
32
-
are given instances in `E`.
30
+
are available as givens in `E`.
33
31
34
32
Like their types, implicit function literals are written with a `given` prefix. They differ from normal function literals in two ways:
35
33
@@ -42,7 +40,7 @@ For example, continuing with the previous definitions,
42
40
43
41
g(22) // is expanded to g(given ev => 22)
44
42
45
-
g(f(2)) // is expanded to g(given ev => f(2) given ev)
43
+
g(f(2)) // is expanded to g(given ev => (f(2) given ev))
46
44
47
45
g(givenctx=> f(22) givenctx) // is left as it is
48
46
```
@@ -90,31 +88,31 @@ that would otherwise be necessary.
90
88
t
91
89
}
92
90
93
-
defrow(init: givenRow=>Unit) where (t: Table) = {
91
+
defrow(init: givenRow=>Unit) given (t: Table) = {
94
92
givenr as Row
95
93
init
96
94
t.add(r)
97
95
}
98
96
99
-
defcell(str: String) where (r: Row) =
97
+
defcell(str: String) given (r: Row) =
100
98
r.add(newCell(str))
101
99
```
102
100
With that setup, the table construction code above compiles and expands to:
103
101
```scala
104
102
table { given ($t: Table) =>
105
103
row { given ($r: Row) =>
106
-
cell("top left") where $r
107
-
cell("top right") where $r
108
-
} where $t
104
+
cell("top left") given$r
105
+
cell("top right") given$r
106
+
} given$t
109
107
row { given ($r: Row) =>
110
-
cell("bottom left") where $r
111
-
cell("bottom right") where $r
112
-
} where $t
108
+
cell("bottom left") given$r
109
+
cell("bottom right") given$r
110
+
} given$t
113
111
}
114
112
```
115
113
### Example: Postconditions
116
114
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 aliases, implicit function types, and extension methods to provide a zero-overhead abstraction.
115
+
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 aliases, implicit function types, and extension methods to provide a zero-overhead abstraction.
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/import-delegate.md
+9-4Lines changed: 9 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,12 @@ Since givens can be anonymous it is not always practical to import them by their
36
36
```scala
37
37
importgivenA.{_:TC}
38
38
```
39
-
This imports any given in `A` that has a type which conforms to `TC`. There can be several bounding types following a `_ :` and bounding types can contain wildcards.
39
+
This imports any given in `A` that has a type which conforms to `TC`. Importing givens of several types `T1,...,Tn`
40
+
is expressed by bounding with a union type.
41
+
```
42
+
import given A.{_: T1 | ... | Tn}
43
+
```
44
+
Importing all instances of a parameterized if expressed by wildcard arguments.
0 commit comments