I've been working a lot with context-parameters, and they're pretty fantastic overall, but I am running into a lot of repetition. For my use case I have one context that I'm passing around to a lot of functions, and I don't really like having to type
context(...: Foo)
over and over again. What if you could create context-aliases that look like keywords? I.e.
Copy code
interface ServerContext
contextalias server = context(serverContext: ServerContext)
server fun foo() { ... } // same as context(_: ServerContext) fun foo()
This one is a bit of a long-shot, and I would be open to alternatives, but something like this would be nice.
y
Youssef Shoaib [MOD]
08/22/2025, 9:12 PM
I imagine this might be hard to implement in the parser.
Is this about providing a name for the
ServerContext
parameter? If so, you can do something like:
Copy code
context(s: ServerContext)
val serverContext get() = s
a
Alexis Manin
08/23/2025, 10:00 AM
I find this proposal pretty elegant. This is very close to what the compiler already does with suspend function (a suspend function have an implicit CoroutineContext receiver). It could help improve expressiveness over context parameters. You could design "thematic" alias (logger, io, etc.).
🙌 1
a
Adrian Stypiński
08/23/2025, 5:49 PM
Tbh for me it would be too vague, especially for ppl that are new to the language. I think current syntax gives just enough info to at least know what’s going on without diving into too much details
âž• 1
y
Youssef Shoaib [MOD]
08/24/2025, 5:34 PM
This seems like a potential usecase for decorators, although I'm not sure what it'd look like
h
Hunter
08/25/2025, 3:29 PM
@Youssef Shoaib [MOD] This isn't about providing a name for the server-context parameter, it's more about having a more succinct and elegant looking api.
j
joseph_ivie
08/25/2025, 3:34 PM
Copy code
context(s: ServerContext) {
fun a(): Int
fun b(input: Int): Int
fun c(input: Int)
}
What about something like this for making multiple decorations? It doesn't solve for lambda types, but it could make sense for everything else
Edit: on second thought, I don't like this. Repetition is better because it's clearer. The aliases is a better solution to this.