jean
10/15/2025, 7:34 AMfun main() {
with(
with("World!") { someWork }
) {
println(feature())
}
}
context(dep1: String)
val someWork: () -> String
get() = {
"Hello $dep1"
}
context(concat: () -> String)
fun feature(): String = "Feature: ${concat()}"
It works but I’m not fan of the nested `with`/`context` and that I have to wrap my functions in getters to be able to pass them around as parameters
I tried to use a regular fun for someWork
and with("World!") { ::someWork }
but I got this error.
> Callable reference to ‘context(dep1: String) fun someWork(): String’ is unsupported because it has context parameters.
Is there a rule of thumb saying lambdas shouldn’t be used as context parameters? I tried this but it doesn’t work neither
fun main() {
val lambda: () -> String = with("World!") {
::someWork
}
println(feature(lambda))
}
context(dep1: String)
fun someWork(): String = "Hello $dep1"
fun feature(concat: () -> String): String = "Feature: ${concat()}"
> Inapplicable candidate(s): context(dep1: String) fun someWork(): StringYoussef Shoaib [MOD]
10/15/2025, 8:07 PM::someWork
doesn't work right now, but it should eventually.
Here's how I'd approach this btw:
Make some fun interface
for concat
so that it's not just a function type. It's rather unusual to have function types and other "primitive-esque" types as contexts.jean
10/15/2025, 8:21 PMcontext(concat: () -> String)
fun feature(): String = "Feature: ${concat()}"
with this?
fun interface Feature() {
operator fun invoke(): String
}
fun Feature(concat: () -> String) = Feature {
"Feature: ${concat()}"
}
jean
10/15/2025, 8:24 PMfun interface
a lot in the last years and I’m trying to see where context-parameters can play a role. There’s some boiler plate associated with fun interface
as seen in the previous snippet and feels a bit overkill for small features that just combine a few building block functionsjean
10/15/2025, 8:25 PMYoussef Shoaib [MOD]
10/15/2025, 8:25 PMinterface Feature {
val value: String
}
fun Feature(concat: String) = object: Feature {
override val value = "Feature: $value"
}
But yes, on the right track.Youssef Shoaib [MOD]
10/15/2025, 8:30 PMcontextHere(foo)
to add a receiver that refers to foo
. This is used in DataFrame
for DSL shenanigans, but this here would be a very simple and natural use for itjean
10/15/2025, 8:34 PMYoussef Shoaib [MOD]
10/15/2025, 8:35 PMYoussef Shoaib [MOD]
10/16/2025, 8:33 PMwith("World!")
with(someWork)
println(feature())