Skip to content

Commit a0e5a8e

Browse files
authored
Merge pull request #8394 from robstoll/context-functions
doc(context fun): fix example, add some more explanations
2 parents f3518f1 + 6593929 commit a0e5a8e

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

docs/docs/reference/contextual/context-functions.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ Their types are _context function types_. Here is an example of a context functi
99
```scala
1010
type Executable[T] = ExecutionContext ?=> T
1111
```
12-
Context function are written using `?=>` as the "arrow" sign.
12+
Context functions are written using `?=>` as the "arrow" sign.
1313
They are applied to synthesized arguments, in
1414
the same way methods with context parameters are applied. For instance:
1515
```scala
1616
given ec as ExecutionContext = ...
1717

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] = ...
1922

2023
f(2)(using ec) // explicit argument
2124
f(2) // argument is inferred
2225
```
2326
Conversely, if the expected type of an expression `E` is a context function type
2427
`(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
2629
```scala
2730
(using x_1: T1, ..., x_n: Tn) => E
2831
```
@@ -40,8 +43,10 @@ For example, continuing with the previous definitions,
4043

4144
g(f(2)) // is expanded to g((using ev: ExecutionContext) => f(2)(using ev))
4245

46+
g(ExecutionContext ?=> f(3)) // is expanded to g((using ev: ExecutionContext) => f(3)(using ev))
4347
g((using ctx: ExecutionContext) => f(22)(using ctx)) // is left as it is
4448
```
49+
4550
### Example: Builder Pattern
4651

4752
Context function types have considerable expressive power. For
@@ -59,7 +64,7 @@ the aim is to construct tables like this:
5964
}
6065
}
6166
```
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
6368
addition of elements via `add`:
6469
```scala
6570
class Table {
@@ -81,7 +86,7 @@ with context function types as parameters to avoid the plumbing boilerplate
8186
that would otherwise be necessary.
8287
```scala
8388
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
8590
init
8691
t
8792
}
@@ -120,7 +125,7 @@ object PostConditions {
120125

121126
def result[T](using r: WrappedResult[T]): T = r
122127

123-
def (x: T).ensuring[T](condition: WrappedResult[T] ?=> Boolean): T = {
128+
def [T] (x: T).ensuring(condition: WrappedResult[T] ?=> Boolean): T = {
124129
assert(condition(using x))
125130
x
126131
}

0 commit comments

Comments
 (0)