commonMain.com.makeevrserg.mobilex.di.Lateinit.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of service-locator Show documentation
Show all versions of service-locator Show documentation
KMM library with frequently used code
The newest version!
package com.makeevrserg.mobilex.di
/**
* [Lateinit] is used for components which can't be initialized internally
* For example: Velocity @inject properties, Android context or spigot plugin instance
*
* It can't be initialized twice and can't be accessed until initialization
*/
class Lateinit : Dependency {
private lateinit var instance: T
fun initialize(value: T) {
check(!::instance.isInitialized) { "Value already initialized" }
this.instance = value
}
fun initialize(factory: Factory) {
val value = factory.create()
initialize(value)
}
override val value: T
get() {
check(::instance.isInitialized) { "Value is not initialized yet" }
return instance
}
}