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

apply() 要らなくない?

apply() 要らなくない?

2017年の夏のKotlin LT祭での発表資料です。
https://kotlin.connpass.com/event/61427/

Motoi Washida

August 22, 2017
Tweet

More Decks by Motoi Washida

Other Decks in Programming

Transcript

  1. BQQMZ ⢪׻זְה // { "key1": 1, "key2": 8 } //

    ΛҾ਺ʹͯؔ͠਺Λݺͼ͍ͨ val map = Bundle() map.putInt("key1", 1) map.putInt("key2", 8) someFunction(map)
  2. BQQMZ ⢪ֲה // { "key1": 1, "key2": 8 } //

    ΛҾ਺ʹͯؔ͠਺Λݺͼ͍ͨ someMethod(Bundle().apply { putInt("key1", 1) putInt("key2", 8) })
  3. ⢽ class Point { var x = 0; var y

    = 0 } var y = 0 // ม਺એݴ val p = Point().apply { x = 3 y = 4 // Ͳͬͪͷ y? } println("p = (${p.x}, ${p.y})") // p = (3, 4) Λग़ྗͦ͠͏
  4. ⢽ class Point { var x = 0; var y

    = 0 } var y = 0 // ม਺એݴ val p = Point().apply { x = 3 y = 4 // Ͳͬͪͷ y? } println("p = (${p.x}, ${p.y})") // p = (3, 0) Λग़ྗ (※)
  5. ➿剏周 class Point { var x = 0; var y

    = 0 } var y = 0 val p = Point().apply { x = 3 y = 4 } println("p = (${p.x}, ${p.y})") // p = (3, 0) Λग़ྗ
  6. ➿剏周 BMTP class Point { var x = 0; var

    y = 0 } var y = 0 val p = Point().also { it.x = 3 it.y = 4 } println("p = (${p.x}, ${p.y})") // p = (3, 4) Λग़ྗ
  7. ➿剏周 class Point { var x = 0; var y

    = 0 } var y = 0 val p = Point().apply { x = 3 y = 4 } println("p = (${p.x}, ${p.y})") // p = (3, 0) Λग़ྗ
  8. ➿剏周 せ⵸➰ֹ䒷侧 class Point(val x: Int, val y: Int) var

    y = 0 val p = Point( x = 3, y = 4 ) println("p = (${p.x}, ${p.y})") // p = (3, 4) Λग़ྗ