Last active
April 5, 2023 10:01
-
-
Save akshaykalola28/2ed417dfc6cca257328e625995ef23db to your computer and use it in GitHub Desktop.
Extensions for the boolean, for different different usecases.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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