class UserController {
// Initialized later (by DI framework, Android lifecycle, etc.)
lateinit var userService: UserService
fun init(svc: UserService) {
userService = svc
}
fun isReady(): Boolean = ::userService.isInitialized // safe check
fun doWork() {
if (!isReady()) error("controller not initialized")
// userService.greet(...)
}
}
class UserService(val name: String)
fun main() {
// repeat — for-loop without an index name
repeat(5) { println("hello $it") }
val c = UserController()
println(c.isReady()) // false
c.init(UserService("alice"))
println(c.isReady()) // true
// Accessing uninitialized lateinit throws UninitializedPropertyAccessException
}
Create a free account and build your private vault. Share publicly whenever you want.