$30 off During Our Annual Pro Sale. View Details »

Kotlin 1.3 さらっとContracts + α

Yoshiyasu KO
December 10, 2018

Kotlin 1.3 さらっとContracts + α

LINE Developers Meetup #47 in Fukuoka

Yoshiyasu KO

December 10, 2018
Tweet

More Decks by Yoshiyasu KO

Other Decks in Programming

Transcript

  1. Kotlin 1.3
    ͞ΒͬͱContracts + α
    LINE Developers Meetup #47 in Fukuoka
    @dashimaki_dofu

    View Slide

  2. ϓϩϑΟʔϧ
    • ໊લ: ߴ Յହ (Yoshiyasu KO)
    • ॴଐ: ϕΨίʔϙϨʔγϣϯ
    • AndroidΤϯδχΞ
    • : @dashimaki_dofu

    View Slide

  3. ϓϩϑΟʔϧ
    ʮ͠ΜͪΌΜͷਓʯͱ֮͑ͯؼ͍ͬͯͩ͘͞
    • ໊લ: ߴ Յହ (Yoshiyasu KO)
    • ॴଐ: ϕΨίʔϙϨʔγϣϯ
    • AndroidΤϯδχΞ
    • : @dashimaki_dofu

    View Slide

  4. What is Kotlin?

    View Slide

  5. Kotlinʢ͜ͱΓΜʣ
    • 2013೥ JetBrains͕ࣾ։ൃͨ͠
    ΦϒδΣΫτࢦ޲ܕݴޠ
    • JVM্Ͱಈ࡞
    • Android Studio 3.0ΑΓ
    ެࣜαϙʔτ

    View Slide

  6. 2018.10.30
    Kotlin 1.3ϦϦʔεʂ

    View Slide

  7. Kotlin 1.3
    • Coroutines ਖ਼ࣜ൛
    • Kotlin/Native β൛
    • Contracts
    • when Ҿ਺ͷείʔϓ࠷దԽ etc...

    View Slide

  8. Kotlin 1.3
    • Coroutines ਖ਼ࣜ൛
    • Kotlin/Native β൛
    • Contracts
    • when Ҿ਺ͷείʔϓ࠷దԽ etc...
    ࠓ೔࿩͢ͷ͸ͪ͜Β

    View Slide

  9. Contracts

    View Slide

  10. Contracts = ܖ໿
    • ؔ਺ͷৼΔ෣͍Λʮܖ໿ʯ͢Δ
    • ίϯύΠϥʹؔ਺ͷৼΔ෣͍Λڭ͑Δ

    View Slide

  11. ?????

    View Slide

  12. Smart Cast
    fun foo(s: String?) {
    if (s != null) s.length
    }

    View Slide

  13. Smart Cast
    fun foo(s: String?) {
    if (s != null) s.length
    } T͸/VMMBCMF

    View Slide

  14. Smart Cast
    fun foo(s: String?) {
    if (s != null) s.length
    }
    /PU/VMMΛอূ

    View Slide

  15. Smart Cast
    fun foo(s: String?) {
    if (s != null) s.length
    }
    /PU/VMMΛอূ ͕ෆཁ

    View Slide

  16. ྫ: isNullOrEmpty()
    fun foo(s: String?) {
    if (!s.isNullOrEmpty()) s!!.length
    }
    ~ Kotlin 1.2

    View Slide

  17. ྫ: isNullOrEmpty()
    fun foo(s: String?) {
    if (!s.isNullOrEmpty()) s!!.length
    }
    T͕ʮOVMMPSۭจࣈʯͰ͋Ε͹USVF
    OVMMͰ΋ۭจࣈͰ΋ͳ͚Ε͹GBMTF
    GBMTFͰ͋Ε͹/PU/VMM͕อূ͞ΕΔ͸ͣ
    ~ Kotlin 1.2

    View Slide

  18. ྫ: isNullOrEmpty()
    fun foo(s: String?) {
    if (!s.isNullOrEmpty()) s!!.length
    }
    ίϯύΠϥ͸JT/VMM0S&NQUZͷʮৼΔ෣͍ʯ͕෼͔Βͳ͍
    4NBSU$BTU͕ޮ͔ͳ͍ɾɾɾ
    ~ Kotlin 1.2

    View Slide

  19. ྫ: isNullOrEmpty()
    fun foo(s: String?) {
    if (!s.isNullOrEmpty()) s.length
    }
    Kotlin 1.3
    JT/VMM0S&NQUZʹ࣮૷͞Εͨ$POUSBDUTʹΑͬͯ
    4NBSU$BTU͕ޮ͘Α͏ʹͳΔʂ

    View Slide

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

    View Slide

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

    View Slide

  22. ྫ: isNullOrEmpty()
    @kotlin.internal.InlineOnly
    public inline fun CharSequence?.isNullOrEmpty(): Boolean {
    contract {
    returns(false) implies (this@isNullOrEmpty != null)
    }
    return this == null || this.length == 0
    }
    %4- ޙड़
    Ͱهड़͞ΕΔ
    Kotlin 1.3

    View Slide

  23. ྫ: isNullOrEmpty()
    @kotlin.internal.InlineOnly
    public inline fun CharSequence?.isNullOrEmpty(): Boolean {
    contract {
    returns(false) implies (this@isNullOrEmpty != null)
    }
    return this == null || this.length == 0
    }
    GBMTFΛฦ࣌͢
    Kotlin 1.3

    View Slide

  24. ྫ: isNullOrEmpty()
    @kotlin.internal.InlineOnly
    public inline fun CharSequence?.isNullOrEmpty(): Boolean {
    contract {
    returns(false) implies (this@isNullOrEmpty != null)
    }
    return this == null || this.length == 0
    }
    GBMTFΛฦ࣌͢ /PU/VMMΛอূ͢Δ͜ͱΛ
    ܖ໿͢Δ
    Kotlin 1.3

    View Slide

  25. ྫ: isNullOrEmpty()
    @kotlin.internal.InlineOnly
    public inline fun CharSequence?.isNullOrEmpty(): Boolean {
    contract {
    returns(false) implies (this@isNullOrEmpty != null)
    }
    return this == null || this.length == 0
    }
    GBMTFΛฦ࣌͢ /PU/VMMΛอূ͢Δ͜ͱΛ
    ܖ໿͢Δ
    ίϯύΠϥʹؔ਺ͷৼΔ෣͍Λ఻͑Δ͜ͱ͕Ͱ͖Δ
    Kotlin 1.3

    View Slide

  26. Contracts DSL
    • returns()
    • ؔ਺ͷ࣮ߦʹ੒ޭͨ࣌͠
    • returns(Boolean?)
    • ؔ਺͕Ҿ਺ͷ஋Λฦͨ࣌͠
    • returnsNotNull()
    • ؔ਺͕NotNullΛฦͨ࣌͠
    • callsInPrace(block, InvocationKind)
    • block ͕ݺ͹ΕΔճ਺Λ InvocationKind Ͱอূ͢Δ

    View Slide

  27. Contracts DSL
    • returns()
    • ؔ਺ͷ࣮ߦʹ੒ޭͨ࣌͠
    • returns(Boolean?)
    • ؔ਺͕Ҿ਺ͷ஋Λฦͨ࣌͠
    • returnsNotNull()
    • ؔ਺͕NotNullΛฦͨ࣌͠
    • callsInPrace(block, InvocationKind)
    • block ͕ݺ͹ΕΔճ਺Λ InvocationKind Ͱอূ͢Δ
    ࣗ෼Ͱ$POUSBDUΛ࡞ΕΔ
    $VTUPN$POUSBDUT

    View Slide

  28. when Ҿ਺ͷείʔϓ࠷దԽ

    View Slide

  29. when Ҿ਺ͷείʔϓ࠷దԽ
    fun Request.getBody() {
    val response = executeRequest()
    return when (response) {
    is Success -> response.body
    is HttpError -> throw HttpException(response.status)
    }
    }
    ~ Kotlin 1.2

    View Slide

  30. when Ҿ਺ͷείʔϓ࠷దԽ
    fun Request.getBody() {
    val response = executeRequest()
    return when (response) {
    is Success -> response.body
    is HttpError -> throw HttpException(response.status)
    }
    }
    ~ Kotlin 1.2
    ϩʔΧϧม਺Λ͜͜Ͱ
    ఆٛ͢Δඞཁ͕͋Δ

    View Slide

  31. when Ҿ਺ͷείʔϓ࠷దԽ
    fun Request.getBody() {
    val response = executeRequest()
    return when (response) {
    is Success -> response.body
    is HttpError -> throw HttpException(response.status)
    }
    }
    ~ Kotlin 1.2
    ϩʔΧϧม਺Λ͜͜Ͱ
    ఆٛ͢Δඞཁ͕͋Δ
    XIFO͚ͩͰ࢖͏ม਺ͷείʔϓ͕޿͕ͬͯ͠·͏

    View Slide

  32. when Ҿ਺ͷείʔϓ࠷దԽ
    fun Request.getBody() =
    when (val response = executeRequest()) {
    is Success -> response.body
    is HttpError -> throw HttpException(response.status)
    }
    Kotlin 1.3

    View Slide

  33. when Ҿ਺ͷείʔϓ࠷దԽ
    fun Request.getBody() =
    when (val response = executeRequest()) {
    is Success -> response.body
    is HttpError -> throw HttpException(response.status)
    }
    Kotlin 1.3
    Ҿ਺಺ͰϩʔΧϧม਺͕
    ఆٛͰ͖Δ

    View Slide

  34. when Ҿ਺ͷείʔϓ࠷దԽ
    fun Request.getBody() =
    when (val response = executeRequest()) {
    is Success -> response.body
    is HttpError -> throw HttpException(response.status)
    }
    Kotlin 1.3
    Ҿ਺಺ͰϩʔΧϧม਺͕
    ఆٛͰ͖Δ
    Ҿ਺Ͱར༻͢Δม਺ͷείʔϓΛ
    XIFOϒϩοΫ಺ͰऩΊΔ͜ͱ͕Ͱ͖Δ
    \

    View Slide

  35. ࠓ೔࿩ͨ͜͠ͱ
    • Contract
    • ίϯύΠϥʹؔ਺ͷʮৼΔ෣͍ʯΛ఻͑Δ
    • ελϯμʔυϥΠϒϥϦʹ΋૊Έࠐ·Ε͍ͯΔ
    • DSL Ͱهड़͞ΕΔ
    • Custom Contracts ΋ఆٛͰ͖Δ
    • when จͷҾ਺ͷείʔϓ࠷దԽ
    • ϩʔΧϧม਺ΛҾ਺಺ͰఆٛͰ͖Δ
    • when ϒϩοΫ಺ʹείʔϓ͕ऩΊΒΕΔ

    View Slide

  36. ·ͱΊ

    View Slide

  37. ͔Ώ͍ॴʹ
    ख͕ಧ͍ͨκʂ

    View Slide

  38. ͡Ό
    ͦΏ͜ͱͰʙ

    View Slide