Kotlin頻出表現

 Kotlin頻出表現

Kotlinでよくつかう文/式等

TwitterID変えました。@lvla0805 -> @MoyuruAizawa

5f533179da1c82722252cbcb93e7356f?s=128

Moyuru Aizawa

November 28, 2016
Tweet

Transcript

  1. ,PUMJOසग़දݱ !MWMB

  2. lvla lvla0805 Ѫᖒ๖ .PZVSV"J[BXB   -FBEFOHJOFFSPGQBJST%JWFVSFLB *OD  FVSFLB,PUMJO&WBOHFMJTU

     +BWB,PUMJO3Y+BWB
  3. None
  4.  QBJST  $PVQMFT  NBUDIHSPVQ *"$  OPXIJSJOH

  5. None
  6. NFFUT

  7. WBM

  8. ‣ SFBEPOMZ ‣ جຊWBMΛར༻͍ͯ͠Δɻ ‣ ňͦΕWBSͰ͋Δඞཁ͋Δͷ ʼn ‣ ΈΜͳ+BWBॻ࣌͘ʹpOBMΛ͚ͭΔΑ͏ʹͳͬͨ WBM

    val readOnliy = 10 readOnliy = 20 // error
  9. /VMMBCMF

  10. ‣ /VMMΛڐ༰͢Δܕͱɺ/VMMΛڋ൱͢Δܕ͕໌֬ʹΘ͚ΒΕ͍ͯΔ ‣ OPU/VMMʹ͸OVMM͸୅ೖͰ͖ͳ͍͠ɺOVMMBCMFΛ୅ೖ͢Δ͜ͱ΋ग़དྷͳ ͍ ‣ ৗʹ/VMMΛҙࣝͯ͠ίʔσΟϯά͢Δ͜ͱʹͳΔ /VMMBCMF val nullable:

    String? = null var notNull: String = null // error notNull: String = nullable // error
  11. 4BGF$BMM

  12. ‣ /VMMBCMF͕/VMMͷ৔߹ɺOVMM͕ฦ٫͞ΕΔɻ ‣ /VMMBCMF͕/VMMͰͳ͍৔߹ɺؔ਺͕࣮ߦ͞ΕΔϓϩύςΟͷ஋͕ฦ٫ ͞ΕΔɻ 4BGF$BMM nullablePerson?.toString() nullablePerson?.age

  13. %FMFHBUFE1SPQFSUJFT

  14. ‣ ϓϩύςΟͷॳظԽΛҕৡͰ͖Δ ‣ ॳظԽΛ஗ԆͰ͖Δ ‣ ϥΠϑαΠΫϧʹґଘ͢Δ΋ͷͰ΋WBMͰॳظԽͰ͖Δ %FMFHBUFE1SPQFSUJFT val view by

    bindView(R.id.view) val string by lazy { getString(R.string.foo) }
  15. &YUFOTJPO'VODUJPO

  16. ‣ ܧঝ͢Δࣄͳ͘ɺطଘΫϥεʹؔ਺Λ௥ՃͰ͖Δɻ &YUFOTJPO'VODUJPO fun Activity.toWeb(uri: Uri) { startActivity( Intent(Intent.ACTION_VIEW, uri)

    ) } fun Subscription.addTo(subscriptions: CompositeSubscription) { subscriptions(this) }
  17. 4JOHMF&YQSFTTJPO'VODUJPO

  18. ‣ ؔ਺ͷ࣮૷͕୯Ұࣜͷ৔߹ɺฦΓ஋ͷએݴɺϒϩοΫɺSFUVSOΛলུ Ͱ͖Δɻ 4JOHMF&YQSFTTJPO'VODUJPO @Provide fun provideApplicationContext() = applicationContext

  19. )JHIFS0SEFS'VODUJPO

  20. ‣ ؔ਺ΛҾ਺ʹͱͬͨΓ ‣ ؔ਺Λฦ٫͢Δؔ਺ ‣ +BWB͸ؔ਺͕ϑΝʔετΫϥεΦϒδΣΫτ͡Όͳ͍͔Β͜Ε͕Ͱ͖ ͳ͍ )JHIFS0SEFS'VODUJPO fun foo(

    function: (Int)->Unit ) { … } fun bar(): (Int)->String { … }
  21. ‣ 7JFXΛҾ਺ʹͱΓ6OJU 㲈7PJE Λฦ٫͢Δؔ਺ͱಉ౳Ͱ͸ 4". public interface OnClickListener { void

    onClick(View var1); }
  22. ‣ ؔ਺ͭͷΠϯλʔϑΣʔεΛҾ਺ʹऔΔؔ਺ʹ͸ɺؔ਺Λ౉ͤ·͢ ‣ هड़ྔΊͬͪΌݮΔʜ 4". view.setOnClickListener({ view -> view.visibility =

    View.GONE }) view.setOnClickListener { view -> view.visibility = View.GONE } view.setOnClickListener { it.visibility = View.GONE }
  23. MFU

  24. ‣ ؔ਺Λ౉͢ͱɺͦͷؔ਺Λ࣮ߦͯ͘͠ΕΔɻ ‣ 4BGF$BMMͰMFUΛ࣮ߦ͍ͯ͠ΔͷͰɺOVMMͰͳ͍৔߹ʹͷΈ࣮ߦ͞Ε Δɻ MFU nullable?.let { it.doSomething }

  25. BQQMZ

  26. ‣ Ϩγʔό 'PP'SBHNFOU ͷϓϩύςΟ΍ؔ਺Λར༻͢Δؔ਺Λ౉͢ ‣ ͦΕΒ͕ద༻͞ΕͨϨγʔό͕ฦ٫͞ΕΔɻ BQQMZ FooFragment().apply { arguments

    = Bundle().apply { pustString("key", "value") } }
  27. ‣ 4JOHMF&YQSFTTJPO'VODUJPOͱ૊Έ߹ΘͤΔͱ͖ͬ͢Γ BQQMZ fun newInstance() = FooFragment().apply { arguments =

    Bundle().apply { pustString("key", "value") } }
  28. EFDMBSJOHQSPQFSUJFTBOE JOJUJBMJ[JOHUIFNGSPNQSJNBSZ DPOTUSVDUPS

  29. ‣ ϓϩύςΟͷએݴͱॳظԽΛɺϓϥΠϚϦίϯετϥΫλͰߦ͑Δ ‣ ϗϯτָʜ EFDMBSJOHQSPQFSUJFTBOEJOJUJBMJ[JOHUIFNGSPNQSJNBSZDPOTUSVDUPS class Person(val firstName: String, val

    lastName: String, var age: Int) { // ... }
  30. %BUB$MBTT

  31. ‣ FRVBMT IBTI$PEF UP4USJOH DPQZ ΛίϯύΠϧ࣌ʹੜ੒ͯ͠ ͘ΕΔ ‣ Ξοϓσʔτ͞Εͳ͍FRVBMT ͱ͞Α͏ͳΒͰ͖Δɻ

    %BUB$MBTT data class Person(val firstName: String, val lastName: String, var age: Int)
  32. OUBSPLOJU

  33. ‣ ֦ுؔ਺Λ࢖ͬͯBTTFSU5IBUΛ͍͍ײ͡ʹͯ͘͠ΕΔ ‣ ΋͏͜Ε͕ͳ͍ͱςετ͸ॻ͚ͳ͍ʜ LOJU @Test fun test() { (1

    + 2).should be 3 // assertThat(1 + 2, `is`(3)) }
  34. TZTZBHJLNPDLJUP

  35. ‣ ֦ுؔ਺Λ࢖ͬͯNPDLJUPΛ͍͍ײ͡ʹͯ͘͠ΕΔɻ ‣ ͜Ε΋ͳ͍ͱςετͰ͖ͳ͍ʜ LNPDLJUP var item: Item = mock()

    item.length().invoked.thenReturn(10) item.length().should be 10 item.verify().length()
  36. 5IBOLZPV