Skip to content

Commit d6bdc74

Browse files
committed
Function applicability
1 parent a70590f commit d6bdc74

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

proposals/context-parameters.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This document is not (yet) formally a KEEP, since it lacks some of the technical
1515

1616
1. Introduction of named context parameters,
1717
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>()`,
1919
4. Contexts are not allowed in constructors,
2020
5. Callable references resolve their context arguments eagerly,
2121
6. Context-in-classes are dropped.
@@ -98,7 +98,7 @@ Note that, like in the case of extension receivers, those types are considered e
9898
fun <A> withConsoleLogger(block: context(Logger) () -> A) = ...
9999

100100
withConsoleLogger {
101-
val logger = context<Logger>()
101+
val logger = implicit<Logger>()
102102
// you can call functions with Logger as context parameter
103103
logWithTime("doing something")
104104
}
@@ -116,10 +116,10 @@ fun <A, B, R> context(a: A, b: B, block: context(A, B) () -> R): R = block(a, b)
116116
fun <A, B, C, R> context(a: A, b: B, c: C, block: context(A, B, C) () -> R): R = block(a, b, c)
117117
```
118118

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.
120120

121121
```kotlin
122-
context(ctx: A) fun <A> context(): A = ctx
122+
context(ctx: A) fun <A> implicit(): A = ctx
123123
```
124124

125125
This function replaces the uses of `this@Type` in the previous iteration of the design.
@@ -376,12 +376,14 @@ functionContext: 'context' [ '(' [ receiverType { ',' receiverType } ] ') ]
376376

377377
**§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.
378378

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:
380382

381383
* Candidates with context parameters are considered more specific than those without them.
382384
* But there is no other prioritization coming from the length of the context parameter list or their types.
383385

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:
385387

386388
* We traverse the tower of scopes looking for **exactly one** default receiver or context parameter with a compatible type.
387389
* Anonymous context parameters (declared with `_`) also participate in this process.
@@ -410,7 +412,7 @@ context(console: ConsoleLogger, file: FileLogger) fun example3() =
410412

411413
### JVM ABI and Java compatibility
412414

413-
**§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.
414416

415417
* Note that parameter names do not impact JVM ABI compatibility.
416418

0 commit comments

Comments
 (0)