https://kotlinlang.org logo
Title
t

tapchicoma

01/12/2018, 7:01 AM
Would you go with 1️⃣ :
nullableValue = NullableValue(1, 2, 3)
nullableValue?.someProperty = 4
or 2️⃣ :
nullableValue = NullableValue(1, 2, 3).apply {
    someProperty = 4
}
where
NullableValue
constructor has a lot of params.
2️⃣ 17
d

dave08

01/14/2018, 9:09 AM
Isn't 2 supposed to be
?.apply
?
t

tapchicoma

01/15/2018, 8:50 AM
Nope, in this case
NullableValue()
is a constructor of object. Example is a little misleading (
d

dave08

01/15/2018, 10:08 AM
So why does 1 have
?.
? Could you show the implementation?
t

tapchicoma

01/15/2018, 10:37 AM
nullableValue
defined as class property:
var nullableValue: NullableValue? = null
b

bdawg.io

01/15/2018, 9:08 PM
I personally opt for
apply
when you’re wanting to call additional methods/properties immediately following the constructor. One potential advantage also being you now have context to the full type returned by the method instead of relying on the potentially more generic property being set to