I am having trouble understanding some code I saw ...
# getting-started
e
I am having trouble understanding some code I saw of the form:
Copy code
fun myfun: Boolean {
  return foo?.stringProperty.isNullOrEmpty()
}
It seems to me that this would return
null
, which is not
Boolean
when
foo
is
null
. Why is the above code legal?
s
Note the lack of
?
before the final function call.
isNullOrEmpty
is an extension on a nullable type, so it can be called with a null receiver and will still return a non-null value.
🙏 1
f
isNullOrEmpty is defined on a nullable type
🙏 1
As in fun String?.fooBar() = if (this != null) foo(this) else bar()