Does anyone know this or do I have to check using ...
# announcements
h
Does anyone know this or do I have to check using javap: if I re-set the value of a String thus:
author = 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?
d
I doubt it, because thats not what you are telling it to do.
Imho you should just use an if-statement instead of trying to write "clever" code
t
It will do something like the following:
Copy code
val 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.
h
Am I not telling the code to do so? That's debatable. Are there cases where there's a difference?
d
If
author
has a custom setter (or even is just a field, maybe volatile) then yes, there is a difference
h
If my
author = 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.
Custom setter, good point.
Even though the compiler could check that too ...