Hildebrandt Tobias
03/27/2026, 10:34 AMfun cTest(
block: context(String) (Int).(props: List<String>) -> Unit
) {
with("Context") {
block(1, listOf("a", "b", "c"))
}
}
val PTest = cTest { props ->
println("Context: ??")
println("Receiver: $this")
println("Parameters: $props")
}
I could do this:
context(str: String)
val PTest get() = cTest { props ->
println("Context: $str")
println("Receiver: $this")
println("Parameters: $props")
}
But then the caller of PTest needs to provide context, which cTest already provides.
I tried searching for this and also asked AI, but came up short.Youssef Shoaib [MOD]
03/27/2026, 10:36 AMcontextOf<String>()Hildebrandt Tobias
03/27/2026, 10:40 AMval PTest = cTest { props ->
println("Context: ${contextOf<String>()}")
println("Receiver: $this")
println("Parameters: $props")
}Hildebrandt Tobias
03/27/2026, 10:40 AMYoussef Shoaib [MOD]
03/27/2026, 10:41 AMcontext(ctx: C)
fun <C> contextOf(): C = ctx
so it just uses the context and returns itHildebrandt Tobias
03/27/2026, 10:44 AMcontext(str: String)
fun String.lambdaTest() { println("Context: $str")}
val PTest = cTest { props ->
println(lambdaTest())
println("Receiver: $this")
println("Parameters: $props")
}
It shows lambdaTest() as unresolved context.Hildebrandt Tobias
03/27/2026, 10:47 AMHildebrandt Tobias
03/27/2026, 10:47 AMString receiver by accident.Hildebrandt Tobias
03/27/2026, 11:50 AMval str = contextOf<String>() in the first row of each one, how would I do that if possible?
So where would I put the context(str: String) then?
Or is there no way to solve this with the context syntax?
val Int.PTest get() = <not here> cTest <also not here> { props ->
println("Context: $str")
println("Receiver: $this")
println("Parameters: $props")
}Youssef Shoaib [MOD]
03/27/2026, 2:54 PMHildebrandt Tobias
03/27/2026, 3:01 PMFC<Props> { } for Components.
With context receivers I was able to just make my own FCs with much used stuff like Themes or Internationalization already inside so that we won't need
useTheme()
useIntl()
usePermissions()
useNavigation()
in every top level component.
Kinda sad to see that go it made them much more compact and readable.
Only alternative would be to have them as additional parameters like props already is.Youssef Shoaib [MOD]
03/27/2026, 3:05 PMinterface Foo {
fun foo()
fun bar(baz: Baz)
}
then you want to make functions like so:
context(f: Foo)
fun foo() = f.foo()
context(f: Foo)
fun bar(baz: Baz) = f.bar(baz)
so, try to consider when exactly you need to call contextOf<Blah>(), and instead, make contextual bridge functions that use Blah as a context and call the functions you want insteadHildebrandt Tobias
03/27/2026, 3:09 PMcontext(theme: Theme)
val spacing get() = theme.spacing
context(theme: Theme)
val shadows get() = theme.shadows
context(theme: Theme)
val palette get() = theme.palette
It may depend on how often we need new fields/functions from these contexts.
For now we don't use that many, but it could also turn out to be a hassle.
In the backend the switch to context parameters was an absolute non-issue.