Skip to content

Instantly share code, notes, and snippets.

@mutkuensert
Created February 19, 2025 11:58
Show Gist options
  • Save mutkuensert/e1467404e17388344ab50eb0cb26f71c to your computer and use it in GitHub Desktop.
Save mutkuensert/e1467404e17388344ab50eb0cb26f71c to your computer and use it in GitHub Desktop.
Useful null check methods
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
@OptIn(ExperimentalContracts::class)
fun allNotNull(
arg1: Any?,
arg2: Any?,
arg3: Any? = Any(),
arg4: Any? = Any(),
arg5: Any? = Any()
): Boolean {
contract {
returns(true) implies (arg1 != null && arg2 != null && arg3 != null && arg4 != null && arg5 != null)
}
return arg1 != null && arg2 != null && arg3 != null && arg4 != null && arg5 != null
}
@OptIn(ExperimentalContracts::class)
fun isAnyNull(
arg1: Any?,
arg2: Any?,
arg3: Any? = Any(),
arg4: Any? = Any(),
arg5: Any? = Any()
): Boolean {
contract {
returns(false) implies (arg1 != null && arg2 != null && arg3 != null && arg4 != null && arg5 != null)
}
return !(arg1 != null && arg2 != null && arg3 != null && arg4 != null && arg5 != null)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment