can you define a context parameter for lambdas? so...
# getting-started
r
can you define a context parameter for lambdas? something like
Copy code
foo(f: context(users: UserService) Bar.() -> Unit)
👌 1
@hfhbd and how does the correct syntax look like?
h
Without the context name
r
isn't it then a context receiver and no longer a context parameter?
can you make an example please, I get a syntax error
h
This seems to work. There may be better ways, context parameters are still new to me.
Copy code
class Foo { val a = 1 }
class Bar { val b = 2 }

fun baz(f: context(Foo) Bar.() -> Int) {
    val foo = Foo()
    val bar = Bar()
    
    context(foo) {
        println(bar.f())
    }
}

fun main() {
    baz {
        contextOf<Foo>().a + b
    }
}
r
I also get
The feature "context receivers" is experimental and should be enabled explicitly
when I use the above, so I am not sure, is it really using context parameters or is the old context receivers which are deprecated?
maybe something with my kotlin setup is wrong 🤔
h
Nope, it is the expected design of context parameters for lambda, see also https://github.com/Kotlin/KEEP/blob/master/proposals/context-parameters.md#declarations-with-context-parameters §1.8
r
true, maybe something with my intellij is wrong then. Will try to update it
was intellij 🙈 now it works as expected