Capturing context parameter

I am investigating the new feature of kotlin, Context parameters.

In some edge situations, you may have a block of code that has accessible some context(s), which are however not directly accessible (not bound to any variable). They are available only for propagating when function with context parameter is called.

Is there some idiomatic way how to access such anonymous context?

To do it, I created a little helper that is as easy as

context(context: C) inline fun <reified C> captureContext() = context

But I wonder if there is some built-in method.

1 Like

I didn’t try it, but the KEEP mentions contextOf<C>().

1 Like

Of course :man_facepalming: I missed that. It works indeed. Thanks.

1 Like