Skip to content

Instantly share code, notes, and snippets.

@adibfara
Last active August 30, 2018 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adibfara/7f860ac6870a5cb1629ec0bbf86d30bb to your computer and use it in GitHub Desktop.
Save adibfara/7f860ac6870a5cb1629ec0bbf86d30bb to your computer and use it in GitHub Desktop.
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
/**
* @author Adib Faramarzi (adibfara@gmail.com)
*/
@ExperimentalContracts
fun createOnce(runFunction: ()-> Unit) {
contract {
callsInPlace(runFunction, kotlin.contracts.InvocationKind.EXACTLY_ONCE)
}
runFunction()
}
@ExperimentalContracts
fun getKotlinVersion(): Float{
val kotlinVersion: Float
createOnce {
kotlinVersion = 1.3f
}
// no compile error!
return kotlinVersion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment