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
A bound like `: Ord` on a type parameter `T` of a method or class indicates an inferred parameter `given Ord[T]`. The inferred parameter(s) generated from context bounds come last in the definition of the containing method or class. E.g.,
13
-
```
12
+
A bound like `: Ord` on a type parameter `T` of a method or class indicates an inferable parameter `given Ord[T]`. The inferable parameter(s) generated from context bounds come last in the definition of the containing method or class. E.g.,
13
+
```scala
14
14
deff[T:C1:C2, U:C3](x: T) given (y: U, z: V):R
15
15
```
16
16
would expand to
17
-
```
17
+
```scala
18
18
deff[T, U](x: T) given (y: U, z: V) givenC1[T], C2[T], C3[U]:R
19
19
```
20
20
Context bounds can be combined with subtype bounds. If both are present, subtype bounds come first, e.g.
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/derivation.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -47,8 +47,8 @@ These two conditions ensure that the synthesized derived instances for the trait
47
47
```scala
48
48
defderived[T] withGeneric[T] = ...
49
49
```
50
-
That is, the `derived` method takes an inferable parameter of type `Generic` that determines the _shape_ of the deriving type `T` and it computes the typeclass implementation according to that shape. A `Generic` instance is generated automatically
51
-
for any types that derives a typeclass that needs it. One can also derive `Generic` alone, which means a `Generic` instance is generated without any other type class instances. E.g.:
50
+
That is, the `derived` method takes an inferable parameter of type `Generic` that determines the _shape_ of the deriving type `T` and it computes the typeclass implementation according to that shape. An implied `Generic` instance is generated automatically for any type that derives a typeclass with a `derived`
51
+
method that refers to `Generic`. One can also derive `Generic` alone, which means a `Generic` instance is generated without any other type class instances. E.g.:
52
52
```scala
53
53
sealedtraitParseResult[T] derivesGeneric
54
54
```
@@ -97,7 +97,7 @@ Cases[(
97
97
Note that an empty element tuple is represented as type `Unit`. A single-element tuple
98
98
is represented as `T *: Unit` since there is no direct syntax for such tuples: `(T)` is just `T` in parentheses, not a tuple.
99
99
100
-
### The Generic TypeClass
100
+
### The Generic Typeclass
101
101
102
102
For every class `C[T_1,...,T_n]` with a `derives` clause, the compiler generates in the companion object of `C` an implied instance of `Generic[C[T_1,...,T_n]]` that follows
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/extension-methods.md
+14-20
Original file line number
Diff line number
Diff line change
@@ -78,39 +78,39 @@ and where `T` is the expected type. The following two rewritings are tried in or
78
78
So `circle.circumference` translates to `CircleOps.circumference(circle)`, provided
79
79
`circle` has type `Circle` and `CircleOps` is an eligible implied instance (i.e. it is visible at the point of call or it is defined in the companion object of `Circle`).
80
80
81
-
## Instances for Extension Methods
81
+
### Implied Instances for Extension Methods
82
82
83
-
Instances that define extension methods can also be defined without an `of` clause. E.g.,
83
+
Implied instances that define extension methods can also be defined without an `of` clause. E.g.,
If such instances are anonymous (as in the examples above), their name is synthesized from the name
97
+
If such implied instances are anonymous (as in the examples above), their name is synthesized from the name
98
98
of the first defined extension method.
99
99
100
100
### Operators
101
101
102
102
The extension method syntax also applies to the definition of operators.
103
103
In each case the definition syntax mirrors the way the operator is applied.
104
104
Examples:
105
-
```
105
+
```scala
106
106
def (x: String) < (y: String) = ...
107
107
def (x: Elem) +: (xs: Seq[Elem]) = ...
108
108
109
109
"ab"+"c"
110
110
1+:List(2, 3)
111
111
```
112
112
The two definitions above translate to
113
-
```
113
+
```scala
114
114
def< (x: String)(y: String) = ...
115
115
def+: (xs: Seq[Elem])(x: Elem) = ...
116
116
```
@@ -120,23 +120,17 @@ to the implementation of right binding operators as normal methods.
120
120
121
121
### Generic Extensions
122
122
123
-
The `StringSeqOps` examples extended a specific instance of a generic type. It is also possible to extend a generic type by adding type parameters to an extension method:
123
+
The `StringSeqOps` examples extended a specific instance of a generic type. It is also possible to extend a generic type by adding type parameters to an extension method. Examples:
As usual, type parameters of the extension method follow the defined method name. Nevertheless, such type parameters can already be used in the preceding parameter clause.
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/inferable-by-name-parameters.md
+6-5
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ layout: doc-page
3
3
title: "Inferable By-Name Parameters"
4
4
---
5
5
6
-
Call-by-name inferable parameters can be used to avoid a divergent inferred expansion.
6
+
Inferable by-name parameters can be used to avoid a divergent inferred expansion. Example:
7
7
8
8
```scala
9
9
traitCodec[T] {
@@ -36,16 +36,17 @@ The precise steps for constructing an inferable argument for a by-name parameter
36
36
1. Create a new implied instance of type `T`:
37
37
38
38
```scala
39
-
implied forT=???
39
+
implied lv forT=???
40
40
```
41
+
where `lv` is an arbitrary fresh name.
41
42
42
-
1. This instance is not immediately available ascandidatefor argument inference (making it immediately available would result in a loop in the synthesized computation). But it becomes available in all nested contexts that look again for an inferred argument to a by-name parameter.
43
+
1. This instance is not immediately available ascandidatefor argument inference (making it immediately available could result in a loop in the synthesized computation). But it becomes available in all nested contexts that look again for an inferred argument to a by-name parameter.
43
44
44
-
1. Ifthis search succeeds with expression `E`, and `E` contains references to the implied instance created previously, replace `E` by
45
+
1. Ifthis search succeeds with expression `E`, and `E` contains references to the implied instance `lv`, replace `E` by
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/query-types.md
+11-10
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,15 @@
1
1
---
2
2
layout: doc-page
3
-
title: "First Class Queries"
3
+
title: "Context Queries"
4
4
---
5
5
6
-
In the context of inference, _queries_ are functions with inferable parameters.
7
-
_Query types_ are the types of first-class queries. Example:
6
+
_Context queries_ are functions with inferable parameters.
7
+
_Context query types_ are the types of first-class context queries.
8
+
Here is an example for a context query type:
8
9
```scala
9
10
typeContextual[T] =givenContext=>T
10
11
```
11
-
A value of query type is applied to inferred arguments, in
12
+
A value of context query type is applied to inferred arguments, in
12
13
the same way a method with inferable parameters is applied. For instance:
13
14
```scala
14
15
implied ctx forContext= ...
@@ -18,9 +19,9 @@ the same way a method with inferable parameters is applied. For instance:
18
19
f(2) givenctx// explicit argument
19
20
f(2) // argument is inferred
20
21
```
21
-
Conversely, if the expected type of an expression `E` is a query
22
+
Conversely, if the expected type of an expression `E` is a context query
22
23
type `given (T_1, ..., T_n) => U` and `E` is not already a
23
-
query literal, `E` is converted to a query literal by rewriting to
24
+
context query literal, `E` is converted to a context query literal by rewriting to
24
25
```scala
25
26
given (x_1: T1, ..., x_n: Tn) =>E
26
27
```
@@ -31,7 +32,7 @@ are available as implied instances in `E`.
31
32
Like query types, query literals are written with a `given` prefix. They differ from normal function literals in two ways:
32
33
33
34
1. Their parameters are inferable.
34
-
2. Their types are query types.
35
+
2. Their types are context query types.
35
36
36
37
For example, continuing with the previous definitions,
37
38
```scala
@@ -45,7 +46,7 @@ For example, continuing with the previous definitions,
45
46
```
46
47
### Example: Builder Pattern
47
48
48
-
Query types have considerable expressive power. For
49
+
Context query types have considerable expressive power. For
49
50
instance, here is how they can support the "builder pattern", where
50
51
the aim is to construct tables like this:
51
52
```scala
@@ -111,7 +112,7 @@ With that setup, the table construction code above compiles and expands to:
111
112
```
112
113
### Example: Postconditions
113
114
114
-
As a larger example, here is a way to define constructs for checking arbitrary postconditions using `ensuring` so that the checked result can be referred to simply by `result`. The example combines opaque aliases, query 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 `ensuring` so that the checked result can be referred to simply by `result`. The example combines opaque aliases, context query types, and extension methods to provide a zero-overhead abstraction.
115
116
116
117
```scala
117
118
objectPostConditions {
@@ -136,7 +137,7 @@ object Test {
136
137
vals=List(1, 2, 3).sum.ensuring(result ==6)
137
138
}
138
139
```
139
-
**Explanations**: We use a query type `given WrappedResult[T] => Boolean`
140
+
**Explanations**: We use a context query type `given WrappedResult[T] => Boolean`
140
141
as the type of the condition of `ensuring`. An argument to `ensuring` such as
141
142
`(result == 6)` will therefore have an implied instance of type `WrappedResult[T]` in
142
143
scope to pass along to the `result` method. `WrappedResult` is a fresh type, to make sure
0 commit comments