Slide 1

Slide 1 text

Contractsで Compilerを賢くしよう Kotlin愛好会 vol5

Slide 2

Slide 2 text

About me •Kenji Abe •Cookpad Inc. •Android Developer •Twitter: @STAR_ZERO

Slide 3

Slide 3 text

Contracts •Kotlin 1.3の新機能 •関数の動作を明示的にCompilerへ伝える •スマートキャストをより便利に

Slide 4

Slide 4 text

今回の環境 •IntelliJ IDEA 2018.2.5 •Kotlin 1.3.0-rc-190

Slide 5

Slide 5 text

簡単な例

Slide 6

Slide 6 text

Contractsなし fun isNonNull(s: String?): Boolean { return s != null } fun hoge(s: String?) { if (isNonNull(s)) { // Smart CastされないのでCompile Error println(s.length) } }

Slide 7

Slide 7 text

Contractsあり @ExperimentalContracts fun isNonNull(s: String?): Boolean { contract { returns(true) implies (s != null) } return s != null }

Slide 8

Slide 8 text

Contractsあり @ExperimentalContracts fun hoge(s: String?) { if (isNonNull(s)) { // Smart Castしてくれる println(s.length) } }

Slide 9

Slide 9 text

Contractsなし fun runFunc(func: () -> Unit) { func() } fun hoge() { var x: Int runFunc { x = 10 } // Variable 'x' must be initialized println(x) }

Slide 10

Slide 10 text

Contractsあり @ExperimentalContracts fun runFunc(func: () -> Unit) { contract { callsInPlace(func, InvocationKind.EXACTLY_ONCE) } func() }

Slide 11

Slide 11 text

Contractsあり @ExperimentalContracts fun hoge() { var x: Int runFunc { x = 10 } // エラーなし println(x) }

Slide 12

Slide 12 text

returns - implies

Slide 13

Slide 13 text

returns - implies @ExperimentalContracts fun isNonNull(s: String?): Boolean { contract { returns(true) implies (s != null) } return s != null } •戻り値によって決まる引数またはthisの状態を定義

Slide 14

Slide 14 text

returns - implies •returns() •returns(value: Any?) ‣ true, false, null •returnsNotNull()

Slide 15

Slide 15 text

returns - implies •implies ‣ == null, != null ‣ is, !is

Slide 16

Slide 16 text

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}") } }

Slide 26

Slide 26 text

callsInPlace

Slide 27

Slide 27 text

callsInPlace @ExperimentalContracts fun runFunc(func: () -> Unit) { contract { callsInPlace(func, InvocationKind.EXACTLY_ONCE) } func() } •引数の関数が何回実行されるか

Slide 28

Slide 28 text

callsInPlace •AT_MOST_ONCE ‣ 1回実行されるか、呼び出されない •AT_LEAST_ONCE ‣ 1回以上呼び出される •EXACTLY_ONCE ‣ 1回だけ呼び出される •UNKNOWN ‣ 呼び出される回数が不明

Slide 29

Slide 29 text

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関数でしか使えない

Slide 43

Slide 43 text

まとめ

Slide 44

Slide 44 text

まとめ •Contractsを使うとComplierがちょっと賢くなる •拡張関数で使うのが現実的な気がする •今後に期待?

Slide 45

Slide 45 text

ありがとうございました