Upgrade to Pro — share decks privately, control downloads, hide ads and more …

5分でわかる Kotlin Contracts

Avatar for kumamotone kumamotone
October 01, 2019

5分でわかる Kotlin Contracts

Avatar for kumamotone

kumamotone

October 01, 2019
Tweet

More Decks by kumamotone

Other Decks in Technology

Transcript

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

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

    ·ͩ&YQFSJNFOUBM͕ͩɺ 4UEMJCͰ΋ͨ͘͞Μ࢖ΘΕ͍ͯΔ
  3. εϚʔτΩϟετ fun getString(s: String?): String { return if (s ==

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

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

    this.isEmpty() } fun getString(s: String?): String { return if (s.isNullOrEmpty()) { "ۭͩΑ" } else { s } } ֦ுؔ਺ʹͯ͠Έͨ
  6. εϚʔτΩϟετʹͳΒͳ͍ྫ 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? ίϯύΠϧΤϥʔ
  7. εϚʔτΩϟετʹͳΒͳ͍ྫ fun String?.isNullOrEmpty(): Boolean { return this == null ||

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

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

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

    implies (this@isNullOrEmpty != null) } return this == null || this.length == 0 } $POUSBDUʰʰܖ໿ʱʱ
  11. @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Ͱ͸ ͳ͍͜ͱΛอূ͢Δʂʱ
  12. @kotlin.internal.InlineOnly public inline fun CharSequence?.isNullOrEmpty(): Boolean { contract { returns(false)

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

    } else { s } } T͸OVMM͕อূ͞ΕͯΔͷͰ εϚʔτΩϟετ ͓͍ͯͨ͠Αʙ ίϯύΠϥ TPTNBSUʜʜ
  14. val answer: Int run { answer = 42 } print(answer)

    run (Kotlin 1.2) ɺଖΕ͸ ੜ໋ɺӉ஦ɺͦͯ͠ສ෺ʹ͍ͭͯͷ ڀۃͷٙ໰ͷ౴͑ʜ ΫΫΫʜ
  15. @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)
  16. @kotlin.internal.InlineOnly public inline fun <T, R> T.run(block: T.() -> R):

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

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

    R { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return block() } Θ͔ͬͨΑʙ ίϯύΠϥ
  19. • 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