Jeff Lockhart
11/05/2021, 10:18 PMHttpClient and an Authenticator class I'd like to install in the client. In an effort to beautify the code, I'm trying to make the syntax concise. I'd like this to work:
class Api(authenticator: Authenticator) {
private val client = HttpClient {
Auth {
authenticator.install() // Unresolved reference: install
}
}
//...
}
class Authenticator(
private val dependency: Dependency
) {
fun Auth.install() {
bearer {
//...
}
}
}
But get "Unresolved reference: install" error. It's not able to call the install() function with Auth as the receiver.
If I change authenticator.install() to with(authenticator) { install() } or authenticator.run { install() }, this works, but obviously not as nice. Authenticator has dependencies, which is why the class is necessary and can't be a simple extension function. Is there a better way to format this code to achieve what I'm trying to do as concisely as possible?rnett
11/06/2021, 12:03 AMclass A { fun B.foo() } expects A in the context, and foo to be called on B. There's no way to change that (see https://youtrack.jetbrains.com/issue/KT-42626), but once context receivers are here you could define it as context(Auth) fun install() and get what you're trying to