新言語KotlinでAndroidプログラミング #DroidKaigi

新言語KotlinでAndroidプログラミング #DroidKaigi

DroidKaigiでの発表で使用したスライドです。

14c9795d267f5b85abb98ca5e8780646?s=128

Taro Nagasawa

April 25, 2015
Tweet

Transcript

  1. 17.

    Hello World (Java) class HelloWorld { public static void main(String[]

    args) { System.out.println("Hello, world!"); } }
  2. 19.

    Hello World (Kotlin) fun main(args: Array<String>) { println("Hello, world!") }

    τοϓϨϕϧʹ ؔ਺Λஔ͚Δ ഑ྻʹ δΣωϦΫε ηϛίϩϯ ෆཁ
  3. 23.

    Ϋϥε (Java) class User { private final long id; private

    final String name; public User(long id, String name) { this.id = id; this.name = Objects.requireNonNull(name); } public long getId() { return id; } public String getName() { return name; } / / toString, equals, hashCodeͱ͔ }
  4. 24.
  5. 25.

    Ϋϥε (Kotlin) class User(val id: Long, val name: String) {

    / / toString, equals, hashCodeͱ͔ } ίϯετϥΫλҾ਺͕ ͦͷ··ϓϩύςΟʹ
  6. 27.

    σʔλΫϥε val taro = User(123, "Taro") println(taro.id) / / =>

    123 println(taro.name) / / => Taro println(taro) / / => User(id=123, name=Taro)
  7. 28.

    ؔ਺ܕΠϯλϑΣʔε (Java) / / anonymous class button.setOnClickListener(new View.OnClickListener() { @Override

    public void onClick(View v) { toast("Clicked!"); } }); ! / / lambda button.setOnClickListener(v -> toast("Clicked!"));
  8. 31.
  9. 32.

    ߴ֊ؔ਺ͰίϨΫγϣϯૢ࡞ (1..999) .filter { it % 3 == 0 ||

    it % 5 == 0 } .reduce { a, b -> a + b } ʮΠϯϥΠϯؔ਺ʯ ؔ਺Ϧςϥϧ͕ల։͞Ε ΦϒδΣΫτΛੜ੒͠ͳ͍
  10. 33.

    ಠࣗศརϝιου (Java) / / ఆٛ static void toast(Context c, String

    s) {...} ! / / ݺͼग़͠ MyUtils.toast(context, "Hello");
  11. 34.

    ಠࣗศརϝιου (Kotlin) / / ఆٛ fun Context.toast(s: String) {...} !

    / / ݺͼग़͠ context.toast("Hello") ! / / ݺͼग़͠(ActivityͳͲͷத) toast("Hello")
  12. 35.

    ಠࣗศརϝιου (Kotlin) / / ఆٛ fun Context.toast(s: String) {...} !

    / / ݺͼग़͠ context.toast("Hello") ! / / ݺͼग़͠(ActivityͳͲͷத) toast("Hello") ϝιουΛੜ΍ͤΔ ʮ֦ுؔ਺ʯ ੩తʹղܾ͞ΕΔʂ
  13. 37.

    Optional (Java) String a = "hoge"; Optional<String> b = Optional.of("fuga");

    Optional<String> c = null; ΦϒδΣΫτੜ੒ ୭ʹ΋OVMM͸ࢭΊΒΕͳ͍ʂ
  14. 38.

    Optional (Kotlin) val a: String = "hoge" val b: String

    = null / / NG ! val c: String? = "hoge" val d: String? = null / / OK ! d.toUpperCase() / / NG
  15. 39.

    NULL҆શ val a: String? = "hoge" val b: String? =

    null ! if (a != null) a.toUpperCase() else null / / => HOGE if (b != null) b.toUpperCase() else null / / => null ! a?.toUpperCase() / / => HOGE b?.toUpperCase() / / => null
  16. 40.

    NULL҆શ val a: String? = "hoge" val b: String? =

    null ! if (a != null) a.toUpperCase() else null / / => HOGE if (b != null) b.toUpperCase() else null / / => null ! a?.toUpperCase() / / => HOGE b?.toUpperCase() / / => null OVMMͰͳ͍͜ͱ͕อূ͞Ε͍ͯΔ ৔ॴͰ͸/PU/VMMܕͱͯ͠ѻ͑Δ
  17. 44.

    KotterKnife ࢖༻લ var button: Button? = null ! button =

    findViewById(R.id.button) as Button ! button?.setOnClickListener {...}
  18. 45.

    KotterKnife ࢖༻લ var button: Button? = null ! button =

    findViewById(R.id.button) as Button ! button?.setOnClickListener {...} "DUJWJUZͱ͔ͷϓϩύςΟ ຖ౓ͷpOE7JFX#Z*E ͦͯ͠Ωϟετʜ
  19. 47.

    KotterKnife ࢖༻ޙ var button: Button by bindView(R.id.button) ! button.setOnClickListener {...}

    ϓϩύςΟ ಉ͡ CZΩʔϫʔυʹଓ͚ͯ 3JECVUUPOΛόΠϯυ pOE7JFX#Z*EෆཁͰ ී௨ʹ࢖͑ΔΑ͏ʹͳͬͯΔ
  20. 49.

    ࢖༻ྫ import kotlinx.android.synthetic.activity_main.button ! public class MainActivity: Activity() { override

    fun onCreate(savedInstaceState: Bundle?) { super.onCreate(savedInstaceState) setContentView(R.layout.activity_main) button.setOnClickListener {...}
  21. 51.