Questions tagged [kotlin-context-receivers]

Context receivers, previously known under the name of "multiple receivers", are designed to make functions, properties, and classes context-dependent (or contextual) by adding context receivers to their declaration. A contextual declaration:

  • Requires all declared context receivers to be present in a caller’s scope as implicit receivers.
  • Brings declared context receivers into its body scope as implicit receivers.

Goals

  • Remove all limitations of member extensions for writing contextual abstractions

    • Support top-level (non-member) contextual functions and properties
    • Support adding contextual function and properties to 3rd party context classes
    • Support multiple contexts
  • Make blocks of code with multiple receivers representable in Kotlin's type system

  • Separate the concepts of extension and dispatch receivers from the concept of context receivers

    • Context receivers should not change the meaning of unqualified this expression
    • Multiple contexts should not be ordered during resolution, resolution ambiguities shall be reported
  • Design a scalable resolution algorithm with respect to the number of receivers

    • Call resolution should not be exponential in the number of context receivers
8 questions
13
votes
4 answers

How to create an extension function with multiple receivers in Kotlin?

I want my extension function to have a couple of receivers. For example, I want function handle to be able to call methods of both CoroutineScope and Iterable instances: fun handle() { // I want to call CoroutineScope.launch() and Iterable.map()…
3
votes
2 answers

How to use functional types or lambdas as type for Kotlin's context receivers?

package yamin typealias Foo = () -> Unit fun main() { bar { baz() } } fun bar(foo: Foo) { foo() } context(Foo) fun baz() { // } I tried to use a lambda type for the context of a function, which seems to be fine at this…
YaMiN
  • 3,194
  • 2
  • 12
  • 36
1
vote
1 answer

How to call Arrow.kt function returning basically Either but using multiple context receivers

Arrow.kt docs point out that instead of: fun get(): Either we can use context(Raise) fun get(): Res And we can even have multiple context receivers. Imagine we have two error types class Err1 class Err2 And a function that has two…
1
vote
1 answer

Context parameter (in context receivers functionality) not showing via reflection

Via reflection, I need to distinguish between a method with a context and a method without a context (Kotlin 1.8.0). Let's say we have following dummy context and a class with two functions - one with the context, second without. class Context(val…
Erlik
  • 658
  • 8
  • 24
1
vote
1 answer

Using context receiver on a delegated property

I have a property delegate using a context receiver: class LoggingPropertyDelegate( private var value: V, private val toLog: T.() -> L ) : ReadWriteProperty { override fun getValue(thisRef: T, property: KProperty<*>)…
alturkovic
  • 990
  • 8
  • 31
0
votes
0 answers

How to make kotlin’s context receiver class work with spring?

Because of some context requirement , I have to mark my spring bean context : context(IConfig) @Named class MyService (...) { val config = SomeBuilder().build() // it needs IConfig context } And the test class : internal class MyServiceTest :…
smallufo
  • 11,516
  • 20
  • 73
  • 111
0
votes
0 answers

Kotlin context receivers in lambdas

I'm trying to use context receivers. I added the freeCompilerArgs = ['-Xcontext-receivers'] to my Kotlin options. Somehow this code doesn't work and I get a casting exception. It doesn't differ from the MySocket context to the CoroutineScope…
0
votes
1 answer

Mocking a service that uses a context receiver Raise using Mockito

Source code available on Github: https://github.com/codependent/context-receiver-sample Suppose you're testing a service ServiceOne that has a dependency on ServiceTwo. ServiceTwo has a method call() with a context receiver of…
codependent
  • 23,193
  • 31
  • 166
  • 308