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
Instead of `A? => B`, use a joint operator `A ?=> B`.
The two separate operators look more familiar, but that
can also be a problem since they suggest that `A?` is
a nullable or in other ways optional type. By joining
`?` and `=>` into a new `?=>` operator we avoid that
misconception and also keep the possibiliy to use postfix
`?` at some point in the future (maybe for the fancy
unboxed options?).
Copy file name to clipboardExpand all lines: docs/docs/reference/contextual/context-functions.md
+17-16Lines changed: 17 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -7,10 +7,11 @@ _Context functions_ are functions with (only) context parameters.
7
7
Their types are _context function types_. Here is an example of a context function type:
8
8
9
9
```scala
10
-
typeExecutable[T] =ExecutionContext?=>T
10
+
typeExecutable[T] =ExecutionContext?=>T
11
11
```
12
-
A context function is applied to synthesized arguments, in
13
-
the same way a method with context parameters is applied. For instance:
12
+
Context function are written using `?=>` as the "arrow" sign.
13
+
They are applied to synthesized arguments, in
14
+
the same way methods with context parameters is applied. For instance:
14
15
```scala
15
16
givenec as ExecutionContext= ...
16
17
@@ -20,26 +21,26 @@ the same way a method with context parameters is applied. For instance:
20
21
f(2) // argument is inferred
21
22
```
22
23
Conversely, if the expected type of an expression `E` is a context function type
23
-
`(T_1, ..., T_n)? => U` and `E` is not already an
24
+
`(T_1, ..., T_n) ?=> U` and `E` is not already an
24
25
context function literal, `E` is converted to an context function literal by rewriting to
25
26
```scala
26
-
(x_1: T1, ..., x_n: Tn)?=>E
27
+
(x_1: T1, ..., x_n: Tn)?=>E
27
28
```
28
29
where the names `x_1`, ..., `x_n` are arbitrary. This expansion is performed
29
30
before the expression `E` is typechecked, which means that `x_1`, ..., `x_n`
30
31
are available as givens in `E`.
31
32
32
-
Like their types, context function literals are written with a `?` after the parameters. They differ from normal function literals in that their types are context function types.
33
+
Like their types, context function literals are written using `?=>` as the arrow between parameters and results. They differ from normal function literals in that their types are context function types.
33
34
34
35
For example, continuing with the previous definitions,
35
36
```scala
36
37
defg(arg: Executable[Int]) = ...
37
38
38
-
g(22) // is expanded to g((ev: ExecutionContext)? => 22)
39
+
g(22) // is expanded to g((ev: ExecutionContext) ?=> 22)
39
40
40
-
g(f(2)) // is expanded to g((ev: ExecutionContext)? => f(2).with(ev))
41
+
g(f(2)) // is expanded to g((ev: ExecutionContext) ?=> f(2).with(ev))
41
42
42
-
g((ctx: ExecutionContext)?=> f(22).with(ctx)) // is left as it is
43
+
g((ctx: ExecutionContext)?=> f(22).with(ctx)) // is left as it is
43
44
```
44
45
### Example: Builder Pattern
45
46
@@ -79,13 +80,13 @@ Then, the `table`, `row` and `cell` constructor methods can be defined
79
80
with context function types as parameters to avoid the plumbing boilerplate
80
81
that would otherwise be necessary.
81
82
```scala
82
-
deftable(init: Table?=>Unit) = {
83
+
deftable(init: Table?=>Unit) = {
83
84
givent as Table
84
85
init
85
86
t
86
87
}
87
88
88
-
defrow(init: Row?=>Unit) with (t: Table) = {
89
+
defrow(init: Row?=>Unit) with (t: Table) = {
89
90
givenr as Row
90
91
init
91
92
t.add(r)
@@ -96,14 +97,14 @@ that would otherwise be necessary.
96
97
```
97
98
With that setup, the table construction code above compiles and expands to:
0 commit comments