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

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 • Coroutines ਖ਼ࣜ൛ • Kotlin/Native β൛ • Contracts

    • when Ҿ਺ͷείʔϓ࠷దԽ etc... ࠓ೔࿩͢ͷ͸ͪ͜Β
  2. Smart Cast fun foo(s: String?) { if (s != null)

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

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

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

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

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

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

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

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

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

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

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

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

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