Created
October 25, 2018 11:49
-
-
Save abohomol/cd67ac7bf048c742fd52b64c12cffe6c to your computer and use it in GitHub Desktop.
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
@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