Let there be a function fooand a class Bar:
fun foo(key: String): String? {
}
class Bar(x: String, y: String) {
}
Now let's specify the code:
val x = foo("x")
val y = foo("y")
if (x.isNotEmpty() && y.isNotEmpty())
return Bar(x, y)
The problem is that this code will not compile. Because this is required Bar(x!!, y!!).
However, when I replace the function with my content, it is !!not needed.
val x = foo("x")
val y = foo("y")
if ((x != null && x.length() > 0) && (y != null && y.length() > 0))
return Bar(x, y)
Why is it impossible to solve a null check from a function .isNotEmpty()?
source
share