Sergio C.
06/16/2019, 12:14 PMkarelpeeters
06/16/2019, 12:16 PMobject {}
things with an operator fun invoke
?Sergio C.
06/16/2019, 12:47 PMkarelpeeters
06/16/2019, 1:14 PMobject
then it'll be a singleton, and using that object in multiple places shouldn't be a problem, there will only be a single instance.karelpeeters
06/16/2019, 1:15 PMSergio C.
06/16/2019, 1:22 PMSergio C.
06/16/2019, 1:23 PMkarelpeeters
06/16/2019, 1:25 PMinvoke
in any way, it's just a normal function. I don't understand where the confusion comes from:
object Test {
fun foo() {
println("I'm $this")
}
}
karelpeeters
06/16/2019, 1:26 PMinvoke
.karelpeeters
06/16/2019, 1:26 PMSergio C.
06/16/2019, 1:50 PMobject ApiController2 {
private var baseUrl: String = "<https://jsonplaceholder.typicode.com/>"
operator fun invoke(baseUrl: String?): AppAPI {
if (baseUrl != null) this.baseUrl = baseUrl
return controller
}
operator fun invoke(): AppAPI {
return controller
}
private val dispatcher: Dispatcher by lazy { Dispatcher() }
private val controller: AppAPI by lazy {
dispatcher.maxRequests = 64
val okHttpClient = OkHttpClient
.Builder()
.dispatcher(dispatcher)
.connectionPool(ConnectionPool())
.build()
Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build()
.create(AppAPI::class.java)
}
}
Sergio C.
06/16/2019, 1:51 PMkarelpeeters
06/16/2019, 1:53 PMlazy
creation be influenced only by the first call to invoke
.karelpeeters
06/16/2019, 1:55 PMkarelpeeters
06/16/2019, 1:55 PMSergio C.
06/16/2019, 1:57 PMSergio C.
06/16/2019, 1:57 PMkarelpeeters
06/16/2019, 1:59 PMApiController2
is a real singleton, but you're using it to create a controller in a strange way, where the first call invoke(url1)
creates a controller, all future calls invoke(url1)
silently ignore the url parameter and just return the previously created object.karelpeeters
06/16/2019, 2:00 PMobject
, the property delegate lazy
and operator overloading invoke
.Sergio C.
06/16/2019, 2:01 PMkarelpeeters
06/16/2019, 2:02 PMSergio C.
06/16/2019, 2:02 PMSergio C.
06/16/2019, 2:02 PM