I'm trying to find the nicest, cleanest way to update a nullable object property that is also an object. Psuedo code:
class MyClass {
val myNullableProperty: MyPropertyClass?
}
Here's the ugly solution I have so far:
val myProperty = myClass.myNullableProperty ?: MyPropertyClass() // MyPropertyClass has default constructor values so this is fine instantiating like this in my example
myProperty.name = "Name"
myClass.myNullableProperty = myProperty
This just feels not the best way of doing this. I feel there's some sort of solution using let/apply/with/run etc. - it feels wasteful to define the local variable myProperty like that, and then set it to myNullableProperty later.
I'm looking for something where I can either retrieve the existing property , or create the new property, and then immediately set the value on that property.