Example 1
@ExperimentalContracts
fun validate(s: String?) {
contract {
returns() implies (s != null)
}
if (s.isNullOrEmpty()) {
throw IllegalArgumentException()
}
}
Slide 17
Slide 17 text
Example 1
@ExperimentalContracts
fun hoge(value: String?) {
validate(value)
println("length = ${value.length}")
}
Slide 18
Slide 18 text
Example 2
@ExperimentalContracts
fun isString(v: Any?): Boolean {
contract {
returns(true) implies (v is String)
}
return v is String
}
Slide 19
Slide 19 text
Example 2
@ExperimentalContracts
fun hoge(v: Any?) {
if (isString(v)) {
println("length = ${v.length}")
}
}
Slide 20
Slide 20 text
Example 3
@ExperimentalContracts
fun castInt(v: Any?): Int? {
contract {
returnsNotNull() implies (v is Int)
}
return v as? Int
}
Slide 21
Slide 21 text
Example 3
@ExperimentalContracts
fun hoge(v: Any?) {
if (castInt(v) != null) {
println("${v + v}")
}
}
Slide 22
Slide 22 text
Example 4
@ExperimentalContracts
fun Any?.isString(): Boolean {
contract {
returns(true) implies (this@isString is String)
}
return this is String
}
Slide 23
Slide 23 text
Example 4
@ExperimentalContracts
fun hoge(v: Any?) {
if (v.isString()) {
println("length = ${v.length}")
}
}
Slide 24
Slide 24 text
Example 5
@ExperimentalContracts
fun stringOrInt(v: Any?) : Boolean {
contract {
returns(true) implies (v is String)
returns(false) implies (v is Int)
}
return v is String
}
Slide 25
Slide 25 text
Example 5
@ExperimentalContracts
fun hoge(v: Any?) {
if (stringOrInt(v)) {
println("length = ${v.length}")
} else {
println("plus = ${1 + v}")
}
}
Example 1
@ExperimentalContracts
fun runAtLeastOnce(func: () -> Unit) {
contract {
callsInPlace(func, InvocationKind.AT_LEAST_ONCE)
}
func()
}
Slide 30
Slide 30 text
Example 1
@ExperimentalContracts
fun hoge() {
var x: Int
runAtLeastOnce {
x = 10
}
println(x)
}
Slide 31
Slide 31 text
Example 2
@ExperimentalContracts
fun runAtMostOnce(func: () -> Unit) {
contract {
callsInPlace(func, InvocationKind.AT_MOST_ONCE)
}
func()
}
Slide 32
Slide 32 text
Example 2
@ExperimentalContracts
fun hoge() {
var x: Int
runAtMostOnce {
x = 10
}
// Variable 'x' must be initialized
println(x)
}
Slide 33
Slide 33 text
Example 3
@ExperimentalContracts
fun runFuncs(func1: () -> Unit, func2: () -> Unit) {
contract {
// func1だけ設定
callsInPlace(func1, InvocationKind.EXACTLY_ONCE)
}
func1()
func2()
}
Slide 34
Slide 34 text
Example 3
@ExperimentalContracts
fun hoge() {
var x: Int
runFuncs({
x = 10
}) {
x = 20
}
// Variable 'x' must be initialized
println(x)
}
Slide 35
Slide 35 text
Example 4
@ExperimentalContracts
fun runFuncs(func1: () -> Unit, func2: () -> Unit) {
contract {
// 今度はfunc2だけ設定
callsInPlace(func2, InvocationKind.EXACTLY_ONCE)
}
func1()
func2()
}
Slide 36
Slide 36 text
Example 4
@ExperimentalContracts
fun hoge() {
var x: Int
runFuncs({
x = 10
}) {
x = 20
}
println(x)
}
Slide 37
Slide 37 text
Example 5
@ExperimentalContracts
fun runFunc(func: () -> Unit) {
contract {
callsInPlace(func, InvocationKind.EXACTLY_ONCE)
}
func()
}
Slide 38
Slide 38 text
Example 5
@ExperimentalContracts
fun hoge() {
var x: Int
runFunc({
x = 10
}) // <- 括弧で括る
// Variable 'x' must be initialized
println(x)
}
Slide 39
Slide 39 text
Example 6
@ExperimentalContracts
fun complex(v: Any?, func: () -> Unit): Boolean {
contract {
returns(true) implies (v is String)
callsInPlace(func, InvocationKind.EXACTLY_ONCE)
}
func()
return v is String
}
Slide 40
Slide 40 text
Example 6
@ExperimentalContracts
fun hoge(v: Any) {
var x: Int
if (complex(v) { x = 10 }) {
println("length = ${v.length}")
}
println("x = $x")
}
Slide 41
Slide 41 text
注意
Slide 42
Slide 42 text
注意
class Helper {
@ExperimentalContracts
fun isNonNull(s: String?): Boolean {
// Contracts are allowed only for top-level functions
contract {
returns(true) implies (s != null)
}
return s != null
}
}
•Top-Level関数でしか使えない