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

Kotlinウォークスルー

 Kotlinウォークスルー

第2回かわいいKotlin勉強会の発表資料です

Taro Nagasawa

July 04, 2014
Tweet

More Decks by Taro Nagasawa

Other Decks in Programming

Transcript

  1. Kotlinͷಛ௃ • ؆ܿ • ҆શ • JVMͰಈ͘ • JavaScriptʹ΋ม׵Ͱ͖Δ •

    ੩తܕ෇͚ • ΦϒδΣΫτࢦ޲ • ؔ਺ܕϓϩάϥϛϯά
  2. Hello World package hello fun main(args : Array<String>) { println("Hello,

    world!") } ύοέʔδ௚Լʹ ؔ਺͕ஔ͚Δ ΤϯτϦϙΠϯτ͸ mainؔ਺
  3. Hello World package hello fun main(args : Array<String>) { println("Hello,

    world!") } ม਺ͷܕ͕ޙஔ ഑ྻ͕ δΣωϦΫε
  4. Ϋϥε class Greeter(val name: String) { fun greet() { println("Hello,

    $name") } } ϓϩύςΟ val g = Greeter(“hoge”) val name = g.name
  5. Ϋϥε class Greeter(val name: String) { fun greet() { println("Hello,

    $name") } } ϝιου ࣜ(ม਺)ΛຒΊࠐΊΔ
  6. ϓϩύςΟ class User(name: String) { var name: String = name

    } ! val user = User("Taro") user.name / / => "Taro"
  7. ϓϩύςΟ class User(name: String) { var name: String = name

    } ! val user = User("Taro") user.name / / => "Taro" ίϯετϥΫλ ϓϩύςΟ
  8. ϓϩύςΟ class User(name: String) { var name: String = name

    } ! val user = User("Taro") user.name / / => "Taro" ϑΟʔϧυʹ௚઀ ΞΫηεͯ͠Δ෩
  9. ΞΫηα class User(name: String) { var name: String = name

    set(name: String) {$name = name } get(): String = $name }
  10. ΞΫηα class User(name: String) { var name: String = name

    set(name: String) {$name = name } get(): String = $name } Setter Getter Backing Field
  11. Delegated Property class User(name: String) { val name: String by

    Decorator(name) } ! class Decorator(val str: String) { fun get(thisRef: Any, prop:PropertyMetadata): String = "<<$str>>" }
  12. Delegated Property class User(name: String) { val name: String by

    Decorator(name) } ! class Decorator(val str: String) { fun get(thisRef: Any, prop:PropertyMetadata): String = "<<$str>>" } User#name΁ͷ ΞΫηε͕ҕৡ͞ΕΔ
  13. Ԡ༻ྫ class MainActivity: Activity() { val submitButton: Button by injector()

    ! fun onCreate(savedInstanceState: Bundle?) { submitButton.setOnClickListener(::submit) } }
  14. ܕ҆શ val a: Array<Int> = array(1, 2, 3) val b:

    Array<Number> = a / / NG val c: Array<out Number> = a / / OK ! a[0] = 5 / / OK c[0] = 5 / / NG
  15. ܕ҆શ val a: Array<Int> = array(1, 2, 3) val b:

    Array<Number> = a / / NG val c: Array<out Number> = a / / OK ! a[0] = 5 / / OK c[0] = 5 / / NG Array͸σϑΥϧτͰ ෆม(invariant) มҐࢦఆ͕Մೳ ͜͜Ͱ͸ڞม(covariant)
  16. ܕ҆શ val a: Array<Int> = array(1, 2, 3) val b:

    Array<Number> = a / / Error val c: Array<out Number> = a / / OK ! a[0] = 5 / / OK c[0] = 5 / / NG ܕͷෆ੔߹Λ ๷͍Ͱ͘ΕΔ
  17. એݴՕॴ෼ࢄ val a: List<Int> = listOf(1, 2, 3) val b:

    List<Number> = a / / OK ———————————————————————— class List<out T> …
  18. એݴՕॴ෼ࢄ val a: List<Int> = listOf(1, 2, 3) val b:

    List<Number> = a ———————————————————————— class List<out T> … Ϋϥεͷએݴ࣌ʹ มҐࢦఆͰ͖Δ
  19. Single Abstract Method / / Java static void invoke(Runnable r)

    { r.run(); } ! / / Kotlin Sample.invoke { hoge() } Runnableͷ୅ΘΓʹ ()->Unitͳؔ਺Λࢦఆ
  20. TCO tailRecursive fun sum(ints: List<Int>, sum: Int = 0): Int

    = if(ints.isEmpty()) sum else sum(ints.tail, sum + ints.first())
  21. TCO tailRecursive fun sum(ints: List<Int>, sum: Int = 0): Int

    = if(ints.isEmpty()) sum else sum(ints.tail, sum + ints.first()) tailRecursive Ξϊςʔγϣϯ
  22. null҆શ val a: Int = null / / OK val

    b: Int? = null / / NG b.toString() / / NG if(b != null) b.toString() / / OK b?.toString() / / OK
  23. null҆શ val a: Int = null / / OK val

    b: Int? = null / / NG b.toString() / / NG if(b != null) b.toString() / / OK b?.toString() / / OK if (b != null) b.toString() else null
  24. ෳࡶ… if(s != null) { val i = toInt(s) if(i

    != null) square(i) else null } else { null }