@@ -9,20 +9,23 @@ Their types are _context function types_. Here is an example of a context functi
9
9
``` scala
10
10
type Executable [T ] = ExecutionContext ?=> T
11
11
```
12
- Context function are written using ` ?=> ` as the "arrow" sign.
12
+ Context functions are written using ` ?=> ` as the "arrow" sign.
13
13
They are applied to synthesized arguments, in
14
14
the same way methods with context parameters are applied. For instance:
15
15
``` scala
16
16
given ec as ExecutionContext = ...
17
17
18
- def f (x : Int ): Executable [Int ] = ...
18
+ def f (x : Int ): ExecutionContext ?=> Int = ...
19
+
20
+ // could be written as follows with the type alias from above
21
+ // def f(x: Int): Executable[Int] = ...
19
22
20
23
f(2 )(using ec) // explicit argument
21
24
f(2 ) // argument is inferred
22
25
```
23
26
Conversely, if the expected type of an expression ` E ` is a context function type
24
27
` (T_1, ..., T_n) ?=> U ` and ` E ` is not already an
25
- context function literal, ` E ` is converted to an context function literal by rewriting to
28
+ context function literal, ` E ` is converted to a context function literal by rewriting it to
26
29
``` scala
27
30
(using x_1 : T1 , ..., x_n : Tn ) => E
28
31
```
@@ -40,8 +43,10 @@ For example, continuing with the previous definitions,
40
43
41
44
g(f(2 )) // is expanded to g((using ev: ExecutionContext) => f(2)(using ev))
42
45
46
+ g(ExecutionContext ?=> f(3 )) // is expanded to g((using ev: ExecutionContext) => f(3)(using ev))
43
47
g((using ctx : ExecutionContext ) => f(22 )(using ctx)) // is left as it is
44
48
```
49
+
45
50
### Example: Builder Pattern
46
51
47
52
Context function types have considerable expressive power. For
@@ -59,7 +64,7 @@ the aim is to construct tables like this:
59
64
}
60
65
}
61
66
```
62
- The idea is to define classes for ` Table ` and ` Row ` that allow
67
+ The idea is to define classes for ` Table ` and ` Row ` that allow the
63
68
addition of elements via ` add ` :
64
69
``` scala
65
70
class Table {
@@ -81,7 +86,7 @@ with context function types as parameters to avoid the plumbing boilerplate
81
86
that would otherwise be necessary.
82
87
``` scala
83
88
def table (init : Table ?=> Unit ) = {
84
- given t as Table
89
+ given t as Table // note the use of a creator application; same as: given t as Table = new Table
85
90
init
86
91
t
87
92
}
@@ -120,7 +125,7 @@ object PostConditions {
120
125
121
126
def result [T ](using r : WrappedResult [T ]): T = r
122
127
123
- def (x : T ).ensuring[ T ] (condition : WrappedResult [T ] ?=> Boolean ): T = {
128
+ def [ T ] (x : T ).ensuring(condition : WrappedResult [T ] ?=> Boolean ): T = {
124
129
assert(condition(using x))
125
130
x
126
131
}
0 commit comments