vide
03/05/2024, 4:11 AMsealed class Parent(val id: String) {
object Child : Parent("child1")
companion object { val subclassId = Child.id }
}
fun main() { Parent.Child }
Joffrey
03/05/2024, 4:13 AMval subclassId: String get() = Child.id
vide
03/05/2024, 4:19 AMlazy
since I wanted to avoid doing some computation on each call (not visible here in the minimal example)Joffrey
03/05/2024, 4:22 AMvide
03/05/2024, 4:27 AMvide
03/05/2024, 4:27 AMRiccardo Lippolis
03/05/2024, 7:41 AMJoffrey
03/05/2024, 9:21 AMChild.id
field access. This would probably be comparable to whatever overhead lazy
adds after initialization (the check that Riccardo mentioned). Hence why I was saying that this doesn't actually save anything to use by lazy { Child.id }
over get() = Child.id
. If anything you're adding extra objects here.
I realized now you were mentioning some other computation that's not in the snippet you shared. In this case, yeah definitely don't worry about `lazy`'s overhead!vide
03/05/2024, 2:55 PMChild.id
) access, but isn't really that heavy anyways. It's more like
val subclassID = lazy { if (blah && /* result of other computation */ && Child.id) { ... } }