Reputation: 8181
I want to negate the following expression:
return SpUtils.loadEMail()?.isEmpty() ?: false
If i add a ! before the expression, like
return !SpUtils.loadEMail()?.isEmpty() ?: false
The IDE(Android Studio) tells me
Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type kotlin.Boolean?
How do I negate this kinds of expressions?
Upvotes: 21
Views: 17322
Reputation: 14085
!(SpUtils().loadEMail()?.isEmpty() ?: false)
would work, but it's really difficult to see when this would actually return true
.
It returns true
when the result of SpUtils().loadEMail()
is null or not empty. With this knowledge, we can easily make something readable:
return SpUtils().loadEMail()?.isNotEmpty() ?: true
You can expand it for even better readability:
val email = SpUtils().loadEMail()
if (email == null || email.isNotEmpty()) {
return true
}
return false
Upvotes: 1
Reputation: 29416
?.
is a safe call operator.
In this case it returns you:
loadEmail()
invocation is not null
null
otherwise!
is a built-in boolean operation which invokes the package.Boolean
's operator called not()
which works only on non-nullable references. The result of ?.
is Boolean?
, thus you get your error.
If you want to negate the whole expression, you can stick to the iRus answer:
!(SpUtils().loadEMail()?.isEmpty() ?: false)
If you want to negate just the SpUtils().loadEMail()?.isEmpty()
part then the correct variant will be:
!(SpUtils().loadEMail()?.isEmpty() ?: true)
If the result of ?.
will be null (there is no mail) then the elvis operator will return you true
(no e-mail) and you'll negate it.
Upvotes: 3
Reputation: 14620
You have problem with nullable reference.
SpUtils.loadEMail()?.isEmpty()
This code produces value of type Boolean? that's mean expression can return an instance of Boolean or null.
I suggest following code to solve your problem:
return !(SpUtils().loadEMail()?.isEmpty() ?: false);
You trying negate Boolean? instead of Boolean, that elvis operator returns!
Upvotes: 15