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

Ankoは甘くて美味しい〜Ankoに見るKotlinの表現力〜 #gunosybeer

Taro Nagasawa
December 08, 2015

Ankoは甘くて美味しい〜Ankoに見るKotlinの表現力〜 #gunosybeer

エムスリー x Gunosy Beer bash!で発表した資料です

Taro Nagasawa

December 08, 2015
Tweet

More Decks by Taro Nagasawa

Other Decks in Programming

Transcript

  1. ඪ४APIͰΰϦΰϦ΍Δ val act = this! val layout = LinearLayout(act)! layout.orientation

    = LinearLayout.VERTICAL! val name = EditText(act)! val button = Button(act)! button.text = "Say Hello"! button.setOnClickListener {! Toast.makeText(act,! "Hello, ${name.text}!”,! Toast.LENGTH_SHORT).show()! }! layout.addView(name)! layout.addView(button)
  2. AnkoͰDSL verticalLayout {! val name = editText()! button("Say Hello") {!

    onClick {! toast("Hello, ${name.text}!”)! }! }! }
  3. ߴ֊ؔ਺ͱϥϜμࣜ // Ҿ਺ʹAΛऔͬͯBΛฦؔ͢਺fΛҾ਺ʹऔΔؔ਺! fun <A, B> convert(target: A,! f: (A)->B)

    = f(target)! ! convert(1, { x: Int -> x * 2 }) //=> 2! convert(2, { x -> x * 2}) //=> 4! convert(3, { it * 2 }) //=> 6! convert(4) { it * 2 } //=> 8
  4. ֦ுؔ਺ × ϥϜμࣜ fun <A, B> A.convert(f: (A)->B) = f(this)!

    ! 3.convert({ x: Int -> x * 2}) //=> 6! 3 convert { it * 2 } //=> 6
  5. Anko class MainActivity : Activity() {! override fun onCreate(savedInstanceState: Bundle?)

    {! super.onCreate(savedInstanceState)! ! verticalLayout {! val name = editText()! button("Say Hello") {! onClick { toast("Hello, ${name.text}!") }! }! }! }! }
  6. Anko class MainActivity : Activity() {! override fun onCreate(savedInstanceState: Bundle?)

    {! super.onCreate(savedInstanceState)! ! verticalLayout {! val name = editText()! button("Say Hello") {! onClick { toast("Hello, ${name.text}!") }! }! }! }! } "DUJWJUZͷ֦ுؔ਺ UIJT͕লུ͞ΕͯΔ
  7. Anko class MainActivity : Activity() {! override fun onCreate(savedInstanceState: Bundle?)

    {! super.onCreate(savedInstanceState)! ! verticalLayout {! val name = editText()! button("Say Hello") {! onClick { toast("Hello, ${name.text}!") }! }! }! }! } ͜ͷUIJT͸ WFSUJDBM-BZPVU
  8. Anko class MainActivity : Activity() {! override fun onCreate(savedInstanceState: Bundle?)

    {! super.onCreate(savedInstanceState)! ! this.verticalLayout {! val name = this.editText()! this.button(“Say Hello") {! onClick { toast("Hello, ${name.text}!") }! }! }! }! } UIJT͕ผͷ΋ͷΛࢦ͢ Կނ͔ʁ
  9. ֦ுؔ਺ΛҾ਺ʹऔΔߴ֊ؔ਺ class Builder { fun foo() {} }! ! fun

    build(f: Builder.() -> Unit) {! Builder().f()! }! ! build {! foo()! }