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

"Convert Java File to Kotlin File"の甘い罠

ukaznil
August 22, 2017

"Convert Java File to Kotlin File"の甘い罠

ukaznil

August 22, 2017
Tweet

More Decks by ukaznil

Other Decks in Programming

Transcript

  1. "Convert Java File to Kotlin File" ͷ؁͍᠘ 2017/08/22 Tue. ՆͷKotlin

    LTࡇ @ukaznil: Tatsuya KONISHI http://ukaznil.com/
  2. ڧ ͍ ਓ "OESPJE4UVEJP*OUFMMJ+*%&"Ͱɼ +BWB͔Β,PUMJOม׵Ͱ͖ΔΜͩɻ ,PUMJOະܦݧ
 ڧؾ ɹ΄Μͱʂʁɹ *%&ศརʔʂɹ͕͢͞+FU#SBJOTʂʂ +BWB͔Βม׵Ͱ͖ΔͳΒ҆৺ͩͶʂ

    ެࣜϓϥάΠϯͩ͠ɻ ,PUMJOະܦݧ ,PUMJO΍ͬͯΈ͍ͨͳʔɻ ڧ ͍ ਓ ʜʜɻ ม׵ʹࣦഊ͢Δ͜ͱ΋͋Δ͔Βɼ ա৴͸ې෺ͳΜͩΑɻ ,PUMJOະܦݧ ઈ๬ ,PUMJOා͍ɻ,PUMJOා͘ͳ͍Α  ,PUMJOා͘ͳ͍Α
  3. Java → Kotlinʹ͍ͭͯɼ Πέͯͳ͍ͳʔʢϋϚΔͳʔʣ ͱࢥ͏ͱ͜ΖΛ঺հ͠·͢ɻ ※࣌ؒͷڐ͢ݶΓ ͬ͘͟Γ֓ཁ • Java →

    Kotlinʹೖ໳͍ͨ͠ਓ
 JavaΛ౿·ͣʹKotlinͷਓ͸ɼ
 ʮΘ͔ΔΘ͔Δʂʂʂʯͱ͸ͳΒͳ͍͔΋͠Ε·ͤΜ͕ɼ
 ࢀߟʹ͸ͳΔͱࢥ͍·͢ɻ •पғʹKotlinΛ෍ڭ͍ͨ͠ਓ λʔήοτ
  4. it's me !! • Android app dev • झຯͰ5೥ڧ •

    Kotlinྺ1.5೥ • ೔ຊAndroidͷձ
 ֶੜ෦OB • ʢ࠷ۙSwift࢝Ίͨʣ • misc • name: Tatsuya KONISHI • web site: http://ukaznil.com/ , Twitter: @ukaznil • job: '17 ৽ଔ > γεςϜ։ൃ / AI Papyrus 
  5. Android Studio: 2.3.3 Kotlin Plugin: 1.1.3-release-Studio2.3-2 ݕূ؀ڥ 1. `get/setXXX` ͷࣗಈੜ੒

    2. ࠐΈೖͬͨ਺஋ͷܕਪ࿦ 3. มଶswitchߏจ X. nullableपΓʢ౰વ͋Δʣ ໨࣍
  6. 1. `get/setXXX` ͷࣗಈੜ੒ class Sample1(private val context: Context): ArrayAdapter<T>(context, R.layout.hoge){

    }  class Sample1 extends ArrayAdapter<T> { private final Context context; public Sample1(Context context) { super(context, R.layout.hoge); this.context = context; } } ʲݪҼʳ ܧঝݩͷArrayAdapterΫϥεʹ ArrayAdapter#getContext() ͳΔϝιου͕ଘࡏ͢Δ͕ɼKotlinͰ͸ϓϩύςΟ `XXX` ʹରͯ͠ɼ setXXX(), getXXX() ͳΔΞΫηα͕ࣗಈੜ੒͞ΕΔʢ࣮ࡍ͸ϑΟʔϧυ΋ʣͨΊɼিಥ͢Δɻ ಉ༷ʹɼϝιου `getText()` ͕͋ΔΫϥεʢex. TextViewʣΛܧঝͨࣗ͠࡞ͷΫϥεʹɼ ϓϩύςΟ `text` ͸࣋ͨͤΒΕͳ͍ɻ < `getContext()`͕িಥͯ͠Δͧʔ
  7. 2. ࠐΈೖͬͨ਺஋ͷܕਪ࿦ private fun sample2() { val score = Random().nextInt(10)

    val point = if (score > 7) 1 else 0.5 val result = point * 100 }  private void sample2() { int score = new Random().nextInt(10); double point = (score > 7) ? 1 : 0.5; double result = point * 100; } ʲݪҼʳ Javaʹ͓͍ͯ͸ int ⊂ double ͕ͩͬͨɼKotlinͰ͸ಠཱͳͷͰɼ ʮฦΓ஋͕Int·ͨ͸DoubleʯˠʮฦΓ஋͸DoubleͰ·ͱΊ͓ͯ͜͏ʯਪ࿦͕ߦΘΕͳ͍ͨΊɻ KotlinͰ͸Ή͠Ζɼ ʮฦΓ஋͕Int·ͨ͸Doubleʯˠʢܧঝؔ܎ͷͳ͍ܕΛҰॹʹฦͯ͘͠ΔͳΑʣ →ʮฦΓ஋͸AnyͰ·ͱΊ͓ͯ͜͏ʯ ͱͳΔɻ ɹɹɹɹɹɹɹŇŇoʢ `toDouble()` Λ࢖͑͹ʣˠˠˠ < ifจͷฦΓ஋ͷܕ͕AnyʹͳͬͯΔͧʔ val point = if (score > 7) 1.toDouble() else 0.5 val result = point * 100
  8. ʲݪҼʳ JavaͰ͸switchจͰ `default` Λઌʹॻ͘͜ͱ͕ڐ͞Ε͍͕ͯͨɼ KotlinͰ͸ͷ `else` ͸࠷ऴจʹ͠ͳ͍ͱౖΒΕΔɻ Javaͷ `final` ͱಉ͘͡ɼKotlinͰ΋

    `msg` ͸ `val` ѻ͍Ͱ͖Δɻ ɹɹɹɹɹɹɹɹɹŇŇoʢ΋͏গ͚ͩ͠៉ྷʹॻ͚Δͳ͊ʣˠˠˠ 3. มଶswitchߏจ private fun sample3() { val score = Random().nextInt(4) val msg: String when (score) { else -> msg = "terrible..." 3 -> msg = "awesome !!!" } }  private void sample3() { int score = new Random().nextInt(4); final String msg; switch (score) { default: msg = "terrible..."; break; case 3: msg = "awesome !!!"; break; } } < `else` ͸࠷ޙʹॻ͔ͳ͍ͱμϝͩͧʔ val msg = when (score) { 3 -> "awesome !!!" else -> "terrible..." }
  9. ·ͱΊ • "Convert Java File to Kotlin File"Ͱੜ੒͞ΕͨKotlin͸ ʮͳΜͪΌͬͯ͜ͱΓΜʯɻա৴͸ې෺ɻ •

    Ͱ΋ษڧʹ͸ͳΔɻॿ͔Δɻ • ίϯόʔλ͕௥͍͔ͭͳ͍ఔɼ
 Kotlinʹ͸ૉ੖Β͍͠ػೳ͕͋ΔʢϙδςΟϒʣɻ
  10. "Convert Java File to Kotlin File" ͷ؁͍᠘ 2017/08/22 Tue. ՆͷKotlin

    LTࡇ @ukaznil: Tatsuya KONISHI http://ukaznil.com/ That's all of