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

JVM言語のかわいいルーキー Kotlinの紹介

JVM言語のかわいいルーキー Kotlinの紹介

DevLOVE現場甲子園2014 東日本大会で発表したときのスライドです。

Taro Nagasawa

August 23, 2014
Tweet

More Decks by Taro Nagasawa

Other Decks in Programming

Transcript

  1. )FMMP8PSME package hello fun main(args : Array<String>) { println("Hello, world!")

    } ύοέʔδ௚Լʹ ؔ਺͕ஔ͚Δ ΤϯτϦϙΠϯτ͸ NBJOؔ਺
  2. 'J[[#V[[ for (i in 1..100) { val result = if

    (i % 15 == 0) "FizzBuzz" else if (i % 3 == 0) "Fizz" else if (i % 5 == 0) "Buzz" else i.toString() println(result) }
  3. 'J[[#V[[ for (i in 1..100) { val result = if

    (i % 15 == 0) "FizzBuzz" else if (i % 3 == 0) "Fizz" else if (i % 5 == 0) "Buzz" else i.toString() println(result) } +BWBͰ͍͏ͱ͜Ζͷ ֦ுGPSจͷΈ 3BOHF ΦϒδΣΫτ
  4. 'J[[#V[[ for (i in 1..100) { val result = if

    (i % 15 == 0) "FizzBuzz" else if (i % 3 == 0) "Fizz" else if (i % 5 == 0) "Buzz" else i.toString() println(result) } ܕਪ࿦ pOBM4USJOHSFTVMUʜ
  5. 'J[[#V[[ for (i in 1..100) { val result = if

    (i % 15 == 0) "FizzBuzz" else if (i % 3 == 0) "Fizz" else if (i % 5 == 0) "Buzz" else i.toString() println(result) } JGFMTF͸ࣜ ஋Λฦ͢
  6. ؔ਺͕pSTUDMBTT val add = { (a: Int, b: Int) ->

    a + b } ! val add2 = add add2(3, 4) / / => 7
  7. ؔ਺͕pSTUDMBTT val add = { (a: Int, b: Int) ->

    a + b } ! val add2 = add add2(3, 4) / / => 7 ؔ਺ϦςϥϧͰ ؔ਺Λੜ੒ͯ͠ ม਺ʹ୅ೖ ී௨ͷม਺ͷ Α͏ʹѻ͑Δ
  8. ؔ਺͕pSTUDMBTT val add = { (a: Int, b: Int) ->

    a + b } ! val add2 = add add2(3, 4) / / => 7 ී௨ͷؔ਺ݺͼग़͠ ͷΑ͏ʹهड़Ͱ͖Δ
  9. ߴ֊ؔ਺ val predicate = { (i: Int) -> i %

    2 != 0 } list.filter(predicate)
  10. ߴ֊ؔ਺ val predicate = { (i: Int) -> i %

    2 != 0 } list.filter(predicate) Ҿ਺͕ح਺ͳΒ USVFͱͳΔड़ޠ ड़ޠͰ΋ͬͯ ϑΟϧλϦϯά
  11. ؔ਺Ϧςϥϧ ϥϜμࣜ list.filter { it % 2 != 0 }

    list.filter({ (i: Int) -> i % 2 != 0 })
  12. ؔ਺Ϧςϥϧ ϥϜμࣜ list.filter { it % 2 != 0 }

    list.filter({ (i: Int) -> i % 2 != 0 }) ҉໧ͷม਺JU ܕ͸ਪ࿦͞ΕΔ
  13. ΠϯϥΠϯؔ਺ val r = list.filter { it % 2 !=

    0 } inline fun filter(p: (T)->Boolean) { … }
  14. ΠϯϥΠϯؔ਺ val r = list.filter { it % 2 !=

    0 } ྫ͑͹ val list_ = arrayListOf<T>() for(e in list) if(e % 2 != 0) list_.add(e) val r = list_ ΠϯϥΠϯల։͞ΕΔ Φʔόϔου௿ݮ inline fun filter(p: (T)->Boolean) { … }
  15. ຤ඌݺग़͠࠷దԽ 5$0 tailRecursive fun sum(ints: List<Int>, s: Int = 0):

    Int = if(ints.none()) s else sum(ints.tail, s + ints.first()) UBJM3FDVSTJWFΞϊςʔγϣϯ ຤ඌͷ࠶ؼݺग़͠Λɺϧʔϓͱͯ͠ ίϯύΠϧͯ͘͠ΕΔ
  16. ϓϩύςΟ class User(name: String) { var name: String = name

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

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

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

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

    set(name: String) {$name = name } get(): String = $name } ϓϩύςΟ໊ CBDLJOHpFME
  21. %FMFHBUFE1SPQFSUZ class User(name: String) { val name: String by Decorator(name)

    } ! class Decorator(val str: String) { fun get(thisRef: Any, prop:PropertyMetadata): String = "˒$str˒" }
  22. %FMFHBUFE1SPQFSUZ class User(name: String) { val name: String by Decorator(name)

    } ! class Decorator(val str: String) { fun get(thisRef: Any, prop:PropertyMetadata): String = "˒$str˒" } 6TFSOBNF΁ͷ ΞΫηε͕ҕৡ͞ΕΔ
  23. ܧঝ open class Foo { fun foo(): String = "foo"

    } ! class Bar: Foo() ! Bar().foo() / / => foo
  24. ܧঝ open class Foo { fun foo(): String = "foo"

    } ! class Bar: Foo() ! Bar().foo() / / => foo PQFOඞਢ σϑΥϧτͰ͸ܧঝෆՄ &⒎FDUJWF+BWB ߲໨ܧঝͷͨΊʹઃܭٴͼจॻԽ͢ΔɺͰͳ͚ Ε͹ܧঝΛېࢭ͢Δ
  25. τϨΠτ trait A { fun hoge() = "A" fun fuga():

    String } ! class Sample: A { override fun fuga() = "Sample" }
  26. τϨΠτ trait Hoge { fun hoge() = "A" fun fuga():

    String } ! class Sample: Hoge { override fun fuga() = "Sample" } GVHB ͸࣮૷ඞਢ PWFSSJEFΞϊςʔγϣϯ
  27. τϨΠτ trait Hoge { fun hoge() = "A" fun fuga():

    String } ! class Sample: Hoge { override fun fuga() = "Sample" } σϑΥϧτ࣮૷Λ ࣋ͯΔ
  28. ଟॏܧঝ trait A { fun a() = "A" } trait

    B { fun a() = "B" } ! class C: A, B { override fun a() = super<A>.a() }
  29. ଟॏܧঝ trait A { fun a() = "A" } trait

    B { fun a() = "B" } ! class C: A, B { override fun a() = super<A>.a() } ಉ໊͡લͷ࣮૷Λ ଟॏܧঝ ಉ໊͡લͷ࣮૷Λ ଟॏܧঝ
  30. ҕৡ class Data(val value: String) ! trait Dao { fun

    save(data: Data) } ! class Service(dao: Dao): Dao by dao { fun createData(value: String) { val data = Data(value) save(data) } }
  31. ҕৡ class Data(val value: String) ! trait Dao { fun

    save(data: Data) } ! class Service(dao: Dao): Dao by dao { fun createData(value: String) { val data = Data(value) save(data) } } ܕ͸ܧঝ͢Δʂ ࣮૷͸ҕৡ͢Δʂ
  32. ԋࢉࢠΦʔόϩʔυ class MyInt(val value: Int) { fun plus(that: MyInt): MyInt

    = MyInt(this.value + that.value) } ! MyInt(5) + MyInt(3) / / => MyInt(8)
  33. ,PUMJOͰ୯७ͳ"DUJWJUZ class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } }
  34. /VMM҆શ val a: Int = null / / OK val

    b: Int? = null / / NG b.toString() / / NG if(b != null) b.toString() / / OK b?.toString() / / OK
  35. /VMM҆શ 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
  36. 4JOHMF"CTUSBDU.FUIPE button.setOnClickListener { Log.d(TAG, "Clicked") } button.setOnClickListener(object: OnClickListener { override

    fun onClick(v: View) { Log.d(TAG, "Clicked") } }) ந৅ϝιουͭͷΈ ؔ਺ϦςϥϧͰදهͰ͖Δ
  37. %FMFHBUFE1SPQFSUZ࠶ͼ class User(name: String) { val name: String by Decorator(name)

    } ! class Decorator(val str: String) { fun get(thisRef: Any, prop:PropertyMetadata): String = "˒$str˒" }
  38. Ԡ༻ྫ7JFXͷ*OKFDU class MainActivity: Activity() { val submitButton: Button by injector()

    ! fun onCreate(savedInstanceState: Bundle?) { submitButton.setOnClickListener(::submit) } }
  39. ,PUMJOͰ#%%ελΠϧͷςετ class TaxCalculatorSpecs: Spek() {{ ! given("Tax rate calculator with

    default locale settings") { val taxRateCalculator = TaxRateCalculator() on("calculating the rate for an income of 200 and an average change of 10 per semester") { val value = taxRateCalculator.calculateRate(200, 10) it("should result in a value of 300") { assertEquals(300, value) } } } }} ςετϑϨʔϜϫʔΫ! Spek http://jetbrains.github.io/spek/ΑΓҾ༻