https://kotlinlang.org logo
#getting-started
Title
# getting-started
h

hfhbd

12/28/2021, 4:05 PM
How can you check, if a class has an initializer for a lateinit variable, when this class is passed as variable to another class?
Copy code
class Config {
    lateinit var s: String
}
class Consumer(val config: Config) {
    init {
         require(config::s.isInitialized)
    }
}
My current workaround is using an internal member function:
fun isInitialized() = ::s.isInitialized
and call this function in
require
.
a

Arxing Lin

12/29/2021, 3:58 AM
Copy code
class Config(var s: String)
can ensure that
s
must have a value
or use
Delegates.notNull<T>()
more elegantly
Copy code
class Config {
  var s: String by Delegates.notNull()
}
m

Mendess

01/03/2022, 11:57 AM
@Arxing Lin what is the point of
Delegates.notNull
? How is it different from lateinit var?
4 Views