Skip to content

Instantly share code, notes, and snippets.

@akshaykalola28
Last active April 5, 2023 10:01
Show Gist options
  • Save akshaykalola28/2ed417dfc6cca257328e625995ef23db to your computer and use it in GitHub Desktop.
Save akshaykalola28/2ed417dfc6cca257328e625995ef23db to your computer and use it in GitHub Desktop.
Extensions for the boolean, for different different usecases.
@file:OptIn(ExperimentalContracts::class)
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
fun Boolean?.isTrue(): Boolean {
contract {
returns(true) implies (this@isTrue != null)
}
return this == true
}
fun Boolean?.isFalse(): Boolean {
contract {
returns(true) implies (this@isFalse != null)
}
return this == false
}
val Boolean?.orTrue: Boolean
get() = this ?: true
val Boolean?.orFalse: Boolean
get() = this ?: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment