0

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.

2 Answers 2

1

Assign the value to itself, or a new instance if it's null:

myClass.myNullableProperty = myClass.myNullableProperty?: MyPropertyClass()

If you want to avoid further null checks:

myClass.myNullableProperty = (myClass.myNullableProperty?: MyPropertyClass()).apply{ 
    name = "Name" 
}

If access to the property is single-threaded, you could avoid all unnecessary assignments this way:

if (myClass.myNullableProperty == null)
    myClass.myNullableProperty = MyPropertyClass()
myClass.myNullableProperty!!.name = "Name"
Sign up to request clarification or add additional context in comments.

1 Comment

Some good thoughts here... I think I like the apply version, I almost came to this solution... forgot about the brackets around the whole elvis operator expression. The second solution you offer is good too, however I think I just hate using !! ... even when I'm certain it's null, it just feels dirty!
0

If you wanna make it more kotlin-y:

myClass.myNullableProperty?.let {
   it.name = "Name"
} ?: run {
   myClass.myNullableProperty = MyPropertyClass().apply {
      name = "Name"
   }
}

8 Comments

let returns null if you don't specify a return value, so the run function would happen regardless of whether the myNullableProperty is null. You would need to replace let with apply or return some non-null value. But this isn't a DRY solution anyway.
@Tenfour04 I don't think that's quite right.. in this instance run will only happen if myNullableProperty is null... the return value of the let is only relevant if there's an aassignment, e.g. val myLetReturnVariable = myClass.myNullableProperty?.let { // etc.
I think this solution works, although as @Tenfour04 mentions it isn't dry with the duplicate name assignment... good suggestion!
@Tenfour04 example does look more concise but I don't like the idea of assigning the object to itself again unnecessarily, just a personal preference.
I did test it, but apparently I did something wrong because I just tested it again and you're right.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.