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
Copy file name to clipboardExpand all lines: proposals/context-parameters.md
+9-7Lines changed: 9 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ This document is not (yet) formally a KEEP, since it lacks some of the technical
15
15
16
16
1. Introduction of named context parameters,
17
17
2. Context receivers are dropped,
18
-
3. Removal of `this@Type` syntax, introduction of `context<A>()`,
18
+
3. Removal of `this@Type` syntax, introduction of `implicit<A>()`,
19
19
4. Contexts are not allowed in constructors,
20
20
5. Callable references resolve their context arguments eagerly,
21
21
6. Context-in-classes are dropped.
@@ -98,7 +98,7 @@ Note that, like in the case of extension receivers, those types are considered e
98
98
fun <A> withConsoleLogger(block: context(Logger) () ->A) =...
99
99
100
100
withConsoleLogger {
101
-
val logger =context<Logger>()
101
+
val logger =implicit<Logger>()
102
102
// you can call functions with Logger as context parameter
103
103
logWithTime("doing something")
104
104
}
@@ -116,10 +116,10 @@ fun <A, B, R> context(a: A, b: B, block: context(A, B) () -> R): R = block(a, b)
116
116
fun <A, B, C, R> context(a:A, b:B, c:C, block: context(A, B, C) () ->R): R= block(a, b, c)
117
117
```
118
118
119
-
**§8***(`context` function)*: We also provide a generic way to obtain a value by type from the context. It allows access to context parameters even when declared using `_`, or within the body of a lambda.
119
+
**§8***(`implicit` function)*: We also provide a generic way to obtain a value by type from the context. It allows access to context parameters even when declared using `_`, or within the body of a lambda.
120
120
121
121
```kotlin
122
-
context(ctx:A) fun <A> context(): A= ctx
122
+
context(ctx:A) fun <A> implicit(): A= ctx
123
123
```
124
124
125
125
This function replaces the uses of `this@Type` in the previous iteration of the design.
**§23***(declaration with context parameters)*: The context parameters declared for a callable are available in the same way as "regular" value parameters in the body of the function. Both value and context parameters are introduced in the same scope, there is no shadowing between them.
378
378
379
-
**§24***(most specific candidate)*: When choosing the **most specific candidate** we follow the Kotlin specification, with one addition:
379
+
**§24***(function applicability)*: Building the constraint system is modified for lambda arguments. Compared with the [Kotlin specification](https://kotlinlang.org/spec/overload-resolution.html#description), the type of the parameter _U<sub>m</sub>_ is replaced with _nocontext(U<sub>m</sub>)_, where _nocontext_ removes the initial `context` block from the function type.
380
+
381
+
**§25***(most specific candidate)*: When choosing the **most specific candidate** we follow the Kotlin specification, with one addition:
380
382
381
383
* Candidates with context parameters are considered more specific than those without them.
382
384
* But there is no other prioritization coming from the length of the context parameter list or their types.
383
385
384
-
**§25***(context resolution)*: Once the overload candidate is chosen, we **resolve** context parameters (if any). For each context parameter:
386
+
**§26***(context resolution)*: Once the overload candidate is chosen, we **resolve** context parameters (if any). For each context parameter:
385
387
386
388
* We traverse the tower of scopes looking for **exactly one** default receiver or context parameter with a compatible type.
387
389
* Anonymous context parameters (declared with `_`) also participate in this process.
**§26***(JVM and Java compatibility)*: In the JVM a function with context parameters is represented as a regular function with the context parameters situated at the *beginning* of the parameter list. The name of the context parameter, if present, is used as the name of the parameter.
415
+
**§27***(JVM and Java compatibility)*: In the JVM a function with context parameters is represented as a regular function with the context parameters situated at the *beginning* of the parameter list. The name of the context parameter, if present, is used as the name of the parameter.
414
416
415
417
* Note that parameter names do not impact JVM ABI compatibility.
0 commit comments