hallvard
04/07/2020, 12:00 PMauthor = someNullableFunction() ?: author
, will the compiler be smart enough to turn the statement into a null check, thus avoiding re-setting a value that is not really changing in situations where the function returns null?diesieben07
04/07/2020, 12:04 PMdiesieben07
04/07/2020, 12:04 PMtseisel
04/07/2020, 12:06 PMval nullableFunctionResult = someNullableFunction()
if (nullableFunctionResult != null) {
author = nullableFunctionResult
} else {
author = author
}
But bytecode optimizers like Proguard/R8, or even the JVM runtime should easily optimize such scenario.hallvard
04/07/2020, 12:08 PMdiesieben07
04/07/2020, 12:08 PMauthor
has a custom setter (or even is just a field, maybe volatile) then yes, there is a differencehallvard
04/07/2020, 12:08 PMauthor = author
case is not optimized away, I will indeed go with Thibault's suggestion, except for the else
part, which is the part I want to avoid anyway. Thanks.hallvard
04/07/2020, 12:10 PMhallvard
04/07/2020, 3:09 PM