Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 849 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to negate a boolean expression when using the elvis operator in kotlin?

#1
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?
Reply

#2
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][1] operator returns!


[1]:

[To see links please register here]

Reply

#3
`?.` is a [safe call operator][1].

In this case it returns you:

* boolean value if the result of `loadEmail()` invocation **is not** `null`
* `null` otherwise

`!` is a [built-in][2] 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.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
`!(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
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through