Ahmed Hassan
12/01/2021, 1:04 PMif (nullableVarParent?.nullableValChildOne == null
|| nullableVarParent?.nullableValChildTwo == null)
return
funcEx(
nullableVarParent?.nullableValChildOne!!,
nullableVarParent?.nullableValChildTwo!!
)
...
I had a discussion with a college about how unsafe the previous code is, to use (!!) operator even after "if condition".
He is totally convinced that the mutable parent is impossible to be null after the if condition since we don't use threads in this area.
His words are some how believable but since everyone in the world has been saying to avoid suppression (!!) in all cases, I wanted to make sure. what do you think might be dangerous about this?Rob Elliot
12/01/2021, 1:06 PMval nullableValChildOne = nullableVarParent?.nullableValChildOne
val nullableValChildTwo = nullableVarParent?.nullableValChildTwo
if (nullableValChildOne == null
|| nullableValChildTwo == null)
return
funcEx(
nullableValChildOne,
nullableValChildTwo
)
Ahmed Hassan
12/01/2021, 1:09 PMRob Elliot
12/01/2021, 1:28 PM!!
then you either need to add an @SuppressWarnings
annotation, which is noisier than the refactor to make the type system happy, or get in the habit of ignoring warnings, which is a root of evil.
There are times when the static type checker is not clever enough, and we need overrides in the form of unchecked casts & not null assertions, but to use them when there’s a trivial way of making the type checker happy is silly.Klitos Kyriacou
12/01/2021, 2:06 PM!!
? I would like to have such warnings too, but I don't see it in the Inspections menu.Rob Elliot
12/01/2021, 2:10 PM!!
. But I don’t now. I’d like to think something has changed, but perhaps I just imagined it… 😳Michael de Kaste
12/02/2021, 9:33 AMwithNoNulls(nullable1, nullable2) { notNull1, notNull2 -> notNull1 to notNull2 } ?: return
makes for very understanding syntax and is scalable
inline fun <R, A, B> withNoNulls(p1: A?, p2: B?, function: (p1: A, p2: B) -> R): R? =
p1?.let { p2?.let { function.invoke(p1, p2) } }
perhaps you CAN convince your colleague to use that and the problem would be inherently solved?