Is it possible to write a helper providing common ...
# getting-started
d
Is it possible to write a helper providing common contexts, e.g. for testing? I was thinking about something like this but was not able to use it properly:
Copy code
fun withTestContext(block: context(Context1, Context2) () -> Unit) =
  block(TEST_CONTEXT_1, TEST_CONTEXT_2)

context(Context1, Context2)
fun myFun() {}

fun myTest() = withTestContext {
  myFun(..., ...) // how to access the corresponding contexts here?
}
y
Copy code
context(A) fun <A> given(): A = this@A
d
That works quite well, although the compiler is unable to infer the types so I have to do
Copy code
myFun(given<Context1>(), given<Context2>())
I hope this will be improved in the next iterations of context parameters.
Moreover, Intellij marks the explicit types as redundant 🤷
e
does
this@Context1
,
this@Context2
not work?
d
does
this@Context1
,
this@Context2
not work
No, but that's probably what I would expect.