Hullaballoonatic
07/01/2019, 8:55 PMclass Foo(bar: Int?, baz: Int?, boz: Int?) {
lateinit var bar: Int = bar // acceptable
lateinit var baz = baz // implicitly assigned as non-nullable
lateinit var boz: Int? = baz // unacceptable
}
Hullaballoonatic
07/01/2019, 8:58 PMinit
block, because I always think it looks really out of place, but as it is currently, afaik, the above logic is only accomplished like this:
class Foo(bar: Int?) {
lateinit var bar: Int
init {
if (bar != null) this.bar = bar
}
}
Pavlo Liapota
07/01/2019, 9:37 PMbar
nullable? 🙂Hullaballoonatic
07/01/2019, 9:38 PMPavlo Liapota
07/01/2019, 9:44 PMlateinit
only in cases when I am sure that variable will be initialized before usage.
How would you work with your variable? You cannot be sure that it will be initialized so would you check isInitialized
?Hullaballoonatic
07/01/2019, 9:45 PMPavlo Liapota
07/01/2019, 9:54 PMclass Foo(
private val _foo: Int?
) {
val foo get() = _foo!!
}
Hullaballoonatic
07/01/2019, 9:54 PMHullaballoonatic
07/01/2019, 9:55 PM_foo
a var, and give it a setter to have the functionality of a lateinit varHullaballoonatic
07/01/2019, 9:56 PM