As of Kotlin 1.3.50
- Contract is experimental
- Used on top-level functions only (cannot be used on class field and property)
- Currently the Kotlin compiler does not verify contracts, so it's a programmer's responsibility to write correct and sound contracts
@UseExperimental(ExperimentalContracts::class)fun FirebaseUser?.isAuthenticated(): Boolean { contract { returns(true) implies (this@isAuthenticated != null) } return this != null}
val auth = FirebaseAuth.getInstance()val user: FirebaseUser? = auth.currentUserif (user.isAuthenticated()) { // further checking is not required as user is guaranteed to be non null val uid: String = user.uid // without contract // val uid: String? = user?.uid}
References: