I've been working a lot with context-parameters, a...
# language-proposals
h
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
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
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
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
This seems like a potential usecase for decorators, although I'm not sure what it'd look like
h
@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
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.