Florian
10/14/2019, 8:56 PMlateinit
or override the getter
. Any other ways to get around immediate initialization? (It's a theoretical question)Nate
10/14/2019, 8:57 PMby lazy
count? Which is a delegate 👇Ruckus
10/14/2019, 8:57 PMFlorian
10/14/2019, 8:58 PMFlorian
10/14/2019, 9:01 PMYago Lasse
10/14/2019, 9:02 PMval
you can initialize it inside the constructor (after some logic) so its kind of obrigatory lazy non lazy hahaFlorian
10/14/2019, 9:03 PMFlorian
10/16/2019, 8:11 AMinit
blocks, right?Ruckus
10/16/2019, 1:40 PMinit
blocks are the constructor (basically).Florian
10/17/2019, 7:48 AMFlorian
10/17/2019, 7:48 AMFlorian
10/17/2019, 7:48 AMRuckus
10/17/2019, 1:46 PMclass Thing(a: Int) {
constructor(a: String) : this(a.length) {
println(a)
}
init {
println(a)
}
}
Thing("thing") // Will print "5" then "thing"
Basically, you need to ensure all properties are properly initialized no matter which constructor is called, which is why they are usually initialized in the primary constructor. You can, however, have a class without a primary constructor, in which case you can initialize the properties in the bodies of the secondary constructors:
class Thing {
val s: String
val i: Int
constructor(a: String) {
s = a
i = a.length
}
constructor(a: Int) {
i = a
s = ".".repeat(a)
}
}
Florian
10/18/2019, 6:04 PM