David Bieregger
11/07/2022, 12:31 PMcontext(Logging, Application)
fun hello() {
<http://logger.info|logger.info>("Hello " + applicationName)
}
Prefered over this:
fun (Logging, Application).hello() {
<http://logger.info|logger.info>("Hello " + applicationName)
}
In my opinion the second one is more readable and makes intuitively more sense when you know how to do one context receiver just put braces around. I'm sure that considerations have been made here, but I can't imagine one real advatage of the first code example over the second onedmitriy.novozhilov
11/07/2022, 12:36 PMdmitriy.novozhilov
11/07/2022, 12:37 PMDavid Bieregger
11/07/2022, 12:42 PMcontext(Application)
fun Logging.hello() {
<http://logger.info|logger.info>("Hello " + applicationName)
}
dmitriy.novozhilov
11/07/2022, 12:44 PMDavid Bieregger
11/07/2022, 12:45 PMBen Woodworth
11/08/2022, 4:39 AMcontext(MyContext)
fun useMyContext() {}
This function can't be used like an extension receiver:
val myContext: MyContext
// Won't compile, since `myContext` is being used as an extension receiver, but a context was expected
myContext.useMyContext()
I'm pretty sure you can't even do this:
with (myContext) {
this.useMyContext()
}
The main distinction is that a function can be called within multiple scopes/contexts, but, you can't really .call()
a function on multiple variables at once. So having Logging
and Application
both as "extension" receivers doesn't make sense. (though it could in the future with tuples)elect
11/09/2022, 5:05 PMcontextalias
similar to typealias