jean
12/19/2025, 10:22 PMcontext(some: String)
val test: () -> Int
get() = { some.toIntOrNull() ?: 0 }
And this seems to be ok for the compiler, it doesn’t complain
val test: context(String) () -> Int = { /*some.toIntOrNull() ?:*/ 0 }
But the problem is that I can’t name the context parameter, I get this error “Named context parameters in function types are unsupported. Use syntax ‘context(Type)’ instead.” Is there a way to access the string from the lambda without naming it?jean
12/19/2025, 10:25 PMfun test(some: String): () -> Int = { some.toIntOrNull() ?: 0 }
But I was not expecting the compiler to be ok with a “non-usable” context parameterYoussef Shoaib [MOD]
12/19/2025, 10:28 PMcontextOf function.
In your example, it can be used like:
val test: context(String) () -> Int = { contextOf<String>().toIntOrNull() ?: 0 }jean
12/19/2025, 10:32 PMcontextOf<String>(), thanks!jean
12/19/2025, 10:34 PMcontext(some: String)
val test: () -> Int = { some.toIntOrNull() ?: 0 }
This gives “Property with context parameters cannot be initialized because it has no backing field”.Youssef Shoaib [MOD]
12/19/2025, 10:40 PMcontext(some: String)
val test: () -> Int get() = { some.toIntOrNull() ?: 0 }
This means that some is eagerly bound when you call test, but the returned value is still a lambda (a no-arg one)jean
12/19/2025, 10:47 PM