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/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