Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin With If Not Null

What would be the most concise way of using with iff a var is not null?

The best I could come up with is:

arg?.let { with(it) {

}}
like image 275
Eliezer Avatar asked Nov 04 '16 01:11

Eliezer


People also ask

How do I check if a variable is not null in Kotlin?

You can use the "?. let" operator in Kotlin to check if the value of a variable is NULL. It can only be used when we are sure that we are refereeing to a non-NULL able value.

IS NOT NULL in Kotlin?

Nullable and Non-Nullable Types in Kotlin – Kotlin type system has distinguish two types of references that can hold null (nullable references) and those that can not (non-null references). A variable of type String can not hold null. If we try to assign null to the variable, it gives compiler error.

IS NOT NULL in if condition?

The IS NOT NULL condition is used in SQL to test for a non-NULL value. It returns TRUE if a non-NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

What does ?: Mean in Kotlin?

This is a binary expression that returns the first operand when the expression value is True and it returns the second operand when the expression value is False. Generally, the Elvis operator is denoted using "?:", the syntax looks like − First operand ?: Second operand.


2 Answers

You can use the Kotlin extension functions apply() or run() depending on whether you want it to be fluent (returning this at end) or transforming (returning a new value at end):

Usage for apply:

something?.apply {
    // this is now the non-null arg
} 

And fluent example:

user?.apply {
   name = "Fred"
   age = 31
}?.updateUserInfo()

Transforming example using run:

val companyName = user?.run {
   saveUser()
   fetchUserCompany()
}?.name ?: "unknown company"

Alternatively if you don't like that naming and really want a function called with() you can easily create your own reusable function:

// returning the same value fluently
inline fun <T: Any> T.with(func: T.() -> Unit): T = this.apply(func)
// or returning a new value
inline fun <T: Any, R: Any> T.with(func: T.() -> R): R = this.func()

Example usage:

something?.with {
    // this is now the non-null arg
}

If you want the null check embedded in the function, maybe a withNotNull function?

// version returning `this` or `null` fluently
inline fun <T: Any> T?.withNotNull(func: T.() -> Unit): T? = 
    this?.apply(func)
// version returning new value or `null`
inline fun <T: Any, R: Any> T?.withNotNull(thenDo: T.() -> R?): R? =
    this?.thenDo()

Example usage:

something.withNotNull {
    // this is now the non-null arg
}

See also:

  • Related functions on Any
  • Kotlin top-level functions
  • Kotlin StdLib API Reference
like image 168
6 revs Avatar answered Sep 25 '22 22:09

6 revs


Looks like the alternative to that would be to use:

arg?.run {

}
like image 41
Eliezer Avatar answered Sep 23 '22 22:09

Eliezer