5分でわかる Kotlin Contracts

266af7918931b0ea15eae9e8f42ecfa6?s=47 kumamotone
October 01, 2019

5分でわかる Kotlin Contracts

266af7918931b0ea15eae9e8f42ecfa6?s=128

kumamotone

October 01, 2019
Tweet

Transcript

  1. 5෼ͰΘ͔Δ Kotlin Contracts 2019/10/01 potatotips #65 twitter.com/kumamo_tone

  2. • iOS/AndroidΤϯδχΞˏϠϑʔ • ษڧձӡӦ • Bonfire iOSɺWWDC Extended ͳͲ •

    ϠϑʔLODGEͰͷpotatotips͸4ճ໨ ۽ຊ ࿨ਖ਼ (@kumamo_tone)
  3. 2019.08 Kotlin 1.3.50 2018.10 Kotlin 1.3 2019.? Kotlin 1.4 όʔδϣϯͷਪҠ

  4. 2019.08 Kotlin 1.3.50 2018.10 Kotlin 1.3 2019.? Kotlin 1.4 όʔδϣϯͷਪҠ

    ৽ػೳ$POUSBDUT͕ొ৔
  5. 2019.08 Kotlin 1.3.50 2018.10 Kotlin 1.3 2019.? Kotlin 1.4 όʔδϣϯͷਪҠ

    ·ͩ&YQFSJNFOUBM͕ͩɺ 4UEMJCͰ΋ͨ͘͞Μ࢖ΘΕ͍ͯΔ
  6. ๩͍͠ਓͷͨΊͷ 5෼ͰΘ͔Δ Kotlin Contracts

  7. • ίϯύΠϥʹ໌ࣔతʹؔ਺ͷ;Δ·͍Λڭ͑Δ΋ͷ • ίϯύΠϥͷ੍໿Λ؇ΊΔ͜ͱ͕Ͱ͖Δ Contracts ͱ͸

  8. • ίϯύΠϥʹ໌ࣔతʹؔ਺ͷ;Δ·͍Λڭ͑Δ΋ͷ • ίϯύΠϥͷ੍໿Λ؇ΊΔ͜ͱ͕Ͱ͖Δ Contracts ͱ͸ ίϯύΠϥͷ੍໿ʁʁʁ

  9. εϚʔτΩϟετ fun getString(s: String?): String { return if (s ==

    null || s.isEmpty()) { "ۭͩΑ" } else { s } }
  10. εϚʔτΩϟετ fun getString(s: String?): String { return if (s ==

    null || s.isEmpty()) { "ۭͩΑ" } else { s } } s ͸ίϯύΠϥʹΑͬͯ String? ͔Β String ʹ Ωϟετ͞Ε͍ͯΔ
  11. εϚʔτΩϟετʹͳΒͳ͍ྫ fun String?.isNullOrEmpty(): Boolean { return this == null ||

    this.isEmpty() } fun getString(s: String?): String { return if (s.isNullOrEmpty()) { "ۭͩΑ" } else { s } } ֦ுؔ਺ʹͯ͠Έͨ
  12. εϚʔτΩϟετʹͳΒͳ͍ྫ fun String?.isNullOrEmpty(): Boolean { return this == null ||

    this.isEmpty() } fun getString(s: String?): String { return if (s.isNullOrEmpty()) { "ۭͩΑ" } else { s } } Type mismatch. Required: String Found: String? ίϯύΠϧΤϥʔ
  13. εϚʔτΩϟετʹͳΒͳ͍ྫ fun String?.isNullOrEmpty(): Boolean { return this == null ||

    this.isEmpty() } fun getString(s: String?): String { return if (s.isNullOrEmpty()) { "ۭͩΑ" } else { s!! } }
  14. εϚʔτΩϟετʹͳΒͳ͍ྫ fun String?.isNullOrEmpty(): Boolean { return this == null ||

    this.isEmpty() } fun getString(s: String?): String { return if (s.isNullOrEmpty()) { "ۭͩΑ" } else { s!! } }
  15. @kotlin.internal.InlineOnly public inline fun CharSequence?.isNullOrEmpty(): Boolean { contract { returns(false)

    implies (this@isNullOrEmpty != null) } return this == null || this.length == 0 }
  16. @kotlin.internal.InlineOnly public inline fun CharSequence?.isNullOrEmpty(): Boolean { contract { returns(false)

    implies (this@isNullOrEmpty != null) } return this == null || this.length == 0 } $POUSBDUʰʰܖ໿ʱʱ
  17. ʰ͜ͷؔ਺͕GBMTFΛฦ͢ͱ͖ɺ ͜ͷ$IBS4FRVFODF͸OVMMͰ͸ ͳ͍͜ͱΛอূ͢Δʂʱ @kotlin.internal.InlineOnly public inline fun CharSequence?.isNullOrEmpty(): Boolean {

    contract { returns(false) implies (this@isNullOrEmpty != null) } return this == null || this.length == 0 }
  18. @kotlin.internal.InlineOnly public inline fun CharSequence?.isNullOrEmpty(): Boolean { contract { returns(false)

    implies (this@isNullOrEmpty != null) } return this == null || this.length == 0 } ʰ͜ͷؔ਺͕GBMTFΛฦ͢ͱ͖ɺ ͜ͷ$IBS4FRVFODF͸OVMMͰ͸ ͳ͍͜ͱΛอূ͢Δʂʱ
  19. @kotlin.internal.InlineOnly public inline fun CharSequence?.isNullOrEmpty(): Boolean { contract { returns(false)

    implies (this@isNullOrEmpty != null) } return this == null || this.length == 0 } Θ͔ͬͨΑʙ ίϯύΠϥ
  20. fun getString(s: String?): String { return if (s.isNullOrEmpty()) { "ۭͩΑ"

    } else { s } } T͸OVMM͕อূ͞ΕͯΔͷͰ εϚʔτΩϟετ ͓͍ͯͨ͠Αʙ ίϯύΠϥ TPTNBSUʜʜ
  21. SVO XJUI BQQMZ BMTP είʔϓؔ਺ ࢛ఱԦ StdlibͰ͔ͭΘΕ͍ͯΔ΄͔ͷྫ

  22. val answer: Int run { answer = 42 } print(answer)

    run (Kotlin 1.2) ɺଖΕ͸ ੜ໋ɺӉ஦ɺͦͯ͠ສ෺ʹ͍ͭͯͷ ڀۃͷٙ໰ͷ౴͑ʜ ΫΫΫʜ
  23. run (Kotlin 1.2) ͳΜʜͩͱʜ ʢେৎ෉ͩͱࢥ͏Μ͚ͩͲͳʜʣ ෳ਺ճݺ͹ΕͨΒ ͋Ϳͳ͍Αʙ ίϯύΠϥ $PNQJMFSFSSPS $BQUVSFEWBMVFTJOJUJBMJ[BUJPO

    JTGPSCJEEFOEVFUPQPTTJCMFSFBTTJHONFOU val answer: Int run { answer = 42 } print(answer)
  24. @kotlin.internal.InlineOnly public inline fun <T, R> T.run(block: T.() -> R)

    = block() @kotlin.internal.InlineOnly public inline fun <T, R> T.run(block: T.() -> R): R { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return block() } run (Kotlin 1.3)
  25. @kotlin.internal.InlineOnly public inline fun <T, R> T.run(block: T.() -> R):

    R { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return block() } $POUSBDUʰʰܖ໿ʱʱ
  26. @kotlin.internal.InlineOnly public inline fun <T, R> T.run(block: T.() -> R):

    R { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return block() } ʰ͜ͷϒϩοΫ͸ʜ ʰͨͬͨ౓ͷΈඞ࣮ͣߦ͞ΕΔʂʂʱʱ
  27. @kotlin.internal.InlineOnly public inline fun <T, R> T.run(block: T.() -> R):

    R { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return block() } Θ͔ͬͨΑʙ ίϯύΠϥ
  28. Ұ౓͔͠ݺ͹Εͳ͍ͳΒ ॳظԽͯ͠΋҆શ͔ͩΒɺ ڐՄ͓͍ͯͨ͠Αʙ ίϯύΠϥ TPTNBSUʜʜ val answer: Int run {

    answer = 42 } print(answer)
  29. ݴ༿ʹ͠ͳ͍ͱɺ఻ΘΒͳ͍͜ͱ΋͋Δʢʁʁʁʣ http://gahag.net/001427-businessman-handshake/

  30. • Contractsͱ͸ • Kotlin 1.3 Ͱ௥Ճ͞ΕͨػೳʢExperimentalʣ • isNullOrEmpty/run/with/apply/alsoͳͲStdlibͰଟ਺࢖ΘΕ͍ͯΔ • ίϯύΠϥʹ໌ࣔతʹؔ਺ͷ;Δ·͍Λڭ͑Δ͜ͱͰɺ


    ίϯύΠϥͷ੍໿Λ؇ΊΔ͜ͱ͕Ͱ͖Δ ·ͱΊ
  31. • What’s New in Kotlin 1.3 • https://kotlinlang.org/docs/reference/whatsnew13.html • What's

    new in Kotlin? • https://speakerdeck.com/svtk/whats-new-in-kotlin • վΊֶͯͿContracts • https://speakerdeck.com/tommykw/gai-metexue-hucontracts References