y
12/14/2023, 1:55 PMclass Foo {
val data: Map<Fizz, Buzz>
init {
data = getData()
doSomeInit() //function uses this.data
}
}
this also compiles, unfortunately
class Foo {
val data: Map<Fizz, Buzz>
init {
doSomeInit() //function uses this.data
data = getData()
}
}
is there any way to at least assert that data
is initialized, like with isInitialized
(which is only available on `lateinit var`s)?ephemient
12/14/2023, 1:58 PMclass Foo(
val data: Map<Fizz, Buzz>
)
or possibly
class Foo private constructor(
val data: Map<Fizz, Buzz>
) {
constructor() : this(mapOf(Fizz to Buzz))
then it will definitely be initialized before any other code in the class bodyy
12/14/2023, 1:59 PMKlitos Kyriacou
12/14/2023, 4:18 PMy
12/14/2023, 4:18 PMKlitos Kyriacou
12/14/2023, 4:19 PMval data = mapOf(Fizz to Buzz)
instead of doing the same thing through a private constructor.y
12/14/2023, 4:20 PMdata
need to be populated for doSomeInit()
to do its thingephemient
12/14/2023, 4:20 PMinit {
doSomeInit()
}
val data = getData()
fun doSomeInit() {
// uses data
ephemient
12/14/2023, 4:21 PMdata
(e.g. null
, bypassing safety checks)y
12/14/2023, 4:24 PM