Kotlin Contract

Oct 18, 2019

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:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.