Kotlin 1.3 さらっとContracts + α

4f9d886a40b9e73b3eaf5b460748684a?s=47 Yoshiyasu KO
December 10, 2018

Kotlin 1.3 さらっとContracts + α

LINE Developers Meetup #47 in Fukuoka

4f9d886a40b9e73b3eaf5b460748684a?s=128

Yoshiyasu KO

December 10, 2018
Tweet

Transcript

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

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

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

    ϕΨίʔϙϨʔγϣϯ • AndroidΤϯδχΞ • : @dashimaki_dofu
  4. What is Kotlin?

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

    3.0ΑΓ ެࣜαϙʔτ
  6. 2018.10.30 Kotlin 1.3ϦϦʔεʂ

  7. Kotlin 1.3 • Coroutines ਖ਼ࣜ൛ • Kotlin/Native β൛ • Contracts

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

    • when Ҿ਺ͷείʔϓ࠷దԽ etc... ࠓ೔࿩͢ͷ͸ͪ͜Β
  9. Contracts

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

  11. ?????

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

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

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

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

    s.length } /PU/VMMΛอূ ͕ෆཁ
  16. ྫ: isNullOrEmpty() fun foo(s: String?) { if (!s.isNullOrEmpty()) s!!.length }

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

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

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

    Kotlin 1.3 JT/VMM0S&NQUZʹ࣮૷͞Εͨ$POUSBDUTʹΑͬͯ 4NBSU$BTU͕ޮ͘Α͏ʹͳΔʂ
  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
  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
  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
  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
  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
  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
  26. Contracts DSL • returns() • ؔ਺ͷ࣮ߦʹ੒ޭͨ࣌͠ • returns(Boolean?) • ؔ਺͕Ҿ਺ͷ஋Λฦͨ࣌͠

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

    • returnsNotNull() • ؔ਺͕NotNullΛฦͨ࣌͠ • callsInPrace(block, InvocationKind) • block ͕ݺ͹ΕΔճ਺Λ InvocationKind Ͱอূ͢Δ ࣗ෼Ͱ$POUSBDUΛ࡞ΕΔ $VTUPN$POUSBDUT
  28. when Ҿ਺ͷείʔϓ࠷దԽ

  29. when Ҿ਺ͷείʔϓ࠷దԽ fun Request.getBody() { val response = executeRequest() return

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

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

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

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

    { is Success -> response.body is HttpError -> throw HttpException(response.status) } Kotlin 1.3 Ҿ਺಺ͰϩʔΧϧม਺͕ ఆٛͰ͖Δ Ҿ਺Ͱར༻͢Δม਺ͷείʔϓΛ XIFOϒϩοΫ಺ͰऩΊΔ͜ͱ͕Ͱ͖Δ \
  35. ࠓ೔࿩ͨ͜͠ͱ • Contract • ίϯύΠϥʹؔ਺ͷʮৼΔ෣͍ʯΛ఻͑Δ • ελϯμʔυϥΠϒϥϦʹ΋૊Έࠐ·Ε͍ͯΔ • DSL Ͱهड़͞ΕΔ

    • Custom Contracts ΋ఆٛͰ͖Δ • when จͷҾ਺ͷείʔϓ࠷దԽ • ϩʔΧϧม਺ΛҾ਺಺ͰఆٛͰ͖Δ • when ϒϩοΫ಺ʹείʔϓ͕ऩΊΒΕΔ
  36. ·ͱΊ

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

  38. ͡Ό ͦΏ͜ͱͰʙ