Skip to content

Instantly share code, notes, and snippets.

@abohomol
Created October 25, 2018 11:49
Show Gist options
  • Save abohomol/cd67ac7bf048c742fd52b64c12cffe6c to your computer and use it in GitHub Desktop.
Save abohomol/cd67ac7bf048c742fd52b64c12cffe6c to your computer and use it in GitHub Desktop.
@ExperimentalContracts
fun notNull(reference: Any?) {
contract {
returns() implies (reference != null)
}
if (reference == null) throw NullPointerException()
}
/* Now you can use the methid as follows: */
fun logUserInfo(user: User?) {
notNull(user)
logger.log(user.id) // Compiler knows `user` is not null here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment