Skip to content

Instantly share code, notes, and snippets.

@hkurokawa
Created February 12, 2019 07:37
Show Gist options
  • Save hkurokawa/315dae5926c9a659eb9fa43b26d89276 to your computer and use it in GitHub Desktop.
Save hkurokawa/315dae5926c9a659eb9fa43b26d89276 to your computer and use it in GitHub Desktop.
Kotlin Contracts sample
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
class Foo {
fun foo() {}
}
@ExperimentalContracts
inline fun Foo.bar(block: Foo.() -> Unit) {
contract {
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
}
block()
this.foo()
}
@ExperimentalContracts
fun hoge(): Int {
Foo().bar {
return 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment