Kotlin Inline Class

Kotlin Inline Class

E28404a128b6014e6f499d2a967c72a0?s=128

Takuji Nishibayashi

April 12, 2019
Tweet

Transcript

  1. KotlinͷInline classΛAndroidΞϓϦ Ͱ׆༻͢Δ @takuji31 Mobile Act OSAKA #9

  2. ࣗݾ঺հ

  3. ࣗݾ঺հ • @takuji31 id:takuji31 • Takuji Nishibayashi • Hatena Co.,

    Ltd. • App Team • ίϛοΫDAYS • δϟϯϓϧʔΩʔʂ • ΧΫϤϜ
  4. ࣗݾ঺հ • Android • Kotlin • Flu-er • ! h-ps:/

    /blog.takuji31.jp • " h-ps:/ /nazuna.takuji31.jp • # $ h-ps:/ /photo.takuji31.jp
  5. ಥવͰ͕࣭͢໰Ͱ͢

  6. AndroidΞϓϦ ։ൃͯ͠·͔͢ʁ

  7. Kotlin ࢖ͬͯ·͢ΑͶʁ

  8. ࢖͍·͢ΑͶʁ

  9. ಛผͳҙຯΛ࣋ͭ ϓϦϛςΟϒܕͷ஋ ͋Γ·͢ΑͶʁ

  10. ྫ͑͹

  11. ID

  12. ೔෇

  13. ೔࣌

  14. URLͷςϯϓϨʔτ

  15. APIͰड͚औͬͨΓ

  16. ը໘ͷύϥϝʔλʔʹͨ͠Γ

  17. ྫ͑͹ID͕શ෦Stringͩͱ Α͘෼͔Βͳ͍

  18. ͏͔ͬΓϢʔβʔͷID౉͢΂͖ͱ͜ Ζʹ ผͷIDΛ౉ͯ͠͠·͏

  19. ܕͰͳΜͱ͔͍ͨ͠

  20. Inline class

  21. Inline class • ࡞Γํ • APIͷϨεϙϯεͰ • ը໘ͷύϥϝʔλʔͱͯ͠

  22. ࡞Γํ

  23. Inline class UserId( val value: String // single value only

    )
  24. ؆୯Ͱ͢Ͷ

  25. APIͷϨεϙϯεͰ

  26. { "id": "1234567890", "name": "takuji31", "iconUrlTemplate": "https://takuji31.jp/icon.png?size={size}", "lastLoggedInAt": "2019-04-09T01:23:45+09:00", "birthday":

    "1987-03-01" }
  27. No Inline classes data class User( val id: String, val

    name: String, val iconUrlTemplate: String, val lastLoggedInAt: String, val birthday: String )
  28. With Inline classes data class User( val id: UserId, val

    name: String, val iconUrlTemplate: UrlTemplate, val lastLoggedInAt: DateTimeString, val birthday: DateString )
  29. Parse͢Δ

  30. kotlinx.serializa.on

  31. Not supported

  32. Gson

  33. JavaͷReflec(onͰ͸ ݩͷܕͱͯ͠ೝࣝ͞ΕΔ →Կ΋͠ͳͯ͘Α͍

  34. Moshi

  35. Reflec%on

  36. Not supported

  37. Codegen

  38. objectͱͯ͠ೝࣝ͠Α͏ͱ͢Δ→ࢮ

  39. AdapterΛ࡞Δ

  40. ࣗಈੜ੒Ͱ͖Δͱ͍͍͔΋ʁ

  41. APIͷϨεϙϯεͰ • ఆٛ͸؆୯ • ࢖͏ͷ΋؆୯ • γϦΞϥΠζ͢Δ࣌͸πʔϧΛબͼͦ͏ • Gson •

    Moshi Codegen + Կ͔ͰAdapterੜ੒
  42. ը໘ͷύϥϝʔλʔͱͯ͠

  43. Jetpack Naviga-on Safe Args

  44. <fragment android:id="@+id/user" android:name="jp.takuji31.inline.UserFragment" > <argument android:name="id" app:argType="jp.takuji31.inline.UserId" /> </fragment>

  45. ͦͷ··ͩͱSafe ArgsʹInline class ͕౉ͤͳ͍

  46. Serializableʹ͢Δ

  47. inline class UserId(val value: String) : Serializable

  48. fun openUserScreen(user: User) { findNavController() .navigate( R.id.action_home_to_user, UserFragmentArgs(user.id).toBundle() ) }

  49. private val args: UserFragmentArgs by navArgs() override fun oncreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) val userId = args.id // do something }
  50. h"ps:/ /github.com/takuji31/inline- class-example

  51. ExperimentalͳͷͰ ͝ར༻͸ܭըతʹ

  52. data classͰ୅༻͢Δͷ΋ ͝ݕ౼͍ͩ͘͞

  53. enjoy Inline class life

  54. ! ࠾༻৘ใ

  55. גࣜձࣾ͸ͯͳͰ͸ɺ ͱ΋ʹʮ௅ઓʯͰ͖Δ ஥ؒΛืू͍ͯ͠·͢

  56. ͜ΜͳਓΛ୳͍ͯ͠·͢ • AndroidΞϓϦΛ࡞Γ͍ͨਓ • iOSΞϓϦΛ࡞Γ͍ͨਓ • ͋Δ͍͸྆ํ • ͳΜͳΒαʔόʔαΠυ΋

  57. h"ps:/ /hatenacorp.jp/recruit/