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

Kotlin/JSと格闘しながらKotlinのイケてる文法を学ぶ

 Kotlin/JSと格闘しながらKotlinのイケてる文法を学ぶ

【とらのあな主催】オタクがKotlinを追うライトニングトークイベントのLT発表資料です

subroh_0508

March 19, 2019
Tweet

More Decks by subroh_0508

Other Decks in Technology

Transcript

  1. ,PUMJOͷΠέͯΔจ๏঺հ w ϥϜμࣜؔ਺Λએݴͤͣɺͦͷ৔Ͱม਺ͷΑ͏ʹ౉͢ // 2つのInt型変数の和を返す val sum = { x:

    Int, y: Int -> x + y } print(sum(3, 4)) // => 7 // 別の関数の引数として渡すことも可 fun addAndTimes(sum: (Int, Int) -> Int, n: Int): Int { return sum(n, n) * n } print(addAndTimes(sum, 5)) // => 50
  2. ,PUMJOͷΠέͯΔจ๏঺հ w ԋࢉࢠΦʔόʔϩʔυԋࢉࢠͷॲཧΛॻ͖׵͑Δ w ීஈ͔͍ͮ͸͋·Γ͠ͳ͍ɺ%4-Λ࡞Δ࣌ʹͱͯ΋ศར data class Person(val name: String)

    data class People(val names: List<String>) // Person同士を足してPeopleに変換して返す operator fun Person.plus(other: Person): People = People(listOf(this.name, other.name)) Person("音無小鳥") + Person("青羽美咲") // => People(["音無小鳥", "青羽美咲"])
  3. ,PUMJO+4Ͱ͸ʜ w 3FBDUͷSFOEFSϝιουΛ,PUMJO+4Ͱॻ͘ͱʜ class App : RComponent<RProps, RState>() { override

    fun RBuilder.render() { div("title") { +"Welcome to React with Kotlin" } } } class App extends React.Component { render() { return ( <div className='title'> Welcome to React with Kotlin </div> ) } } ,PUMJO+4 3FBDU
  4. ,PUMJO+4Ͱ͸ʜ w 3FBDUͷSFOEFSϝιουΛ,PUMJO+4Ͱॻ͘ͱʜ class App : RComponent<RProps, RState>() { override

    fun RBuilder.render() { div("title") { +"Welcome to React with Kotlin" } } }
  5. ,PUMJO+4Ͱ͸ʜ w 3FBDUͷSFOEFSϝιουΛ,PUMJO+4Ͱॻ͘ͱʜ class App : RComponent<RProps, RState>() { override

    fun RBuilder.render() { div("title") { +"Welcome to React with Kotlin" } } } SFOEFSϝιου͕ ʮ3#VJMEFSΫϥεͷ֦ுؔ਺ʯ ͱͯ͠ఆٛ
  6. ,PUMJO+4Ͱ͸ʜ w 3FBDUͷSFOEFSϝιουΛ,PUMJO+4Ͱॻ͘ͱʜ class App : RComponent<RProps, RState>() { override

    fun RBuilder.render() { div("title") { +"Welcome to React with Kotlin" } } } EJW ϝιου ˠEJWλάΛߏங͢Δ
  7. fun RBuilder.div( classes: String? = null, block: RDOMBuilder<DIV>.() -> Unit

    ): ReactElement = … ,PUMJO+4Ͱ͸ʜ w 3FBDUͷSFOEFSϝιουΛ,PUMJO+4Ͱॻ͘ͱʜ class App : RComponent<RProps, RState>() { override fun RBuilder.render() { div("title") { +"Welcome to React with Kotlin" } } } EJW ϝιου ˠ3#VJMEFSΫϥεͷ֦ுؔ਺
  8. fun RBuilder.div( classes: String? = null, block: RDOMBuilder<DIV>.() -> Unit

    ): ReactElement = … ,PUMJO+4Ͱ͸ʜ w 3FBDUͷSFOEFSϝιουΛ,PUMJO+4Ͱॻ͘ͱʜ class App : RComponent<RProps, RState>() { override fun RBuilder.render() { div("title") { +"Welcome to React with Kotlin" } } } SFOEFS είʔϓ಺ͷUIJT͸ 3#VJMEFSΫϥε ˠUIJTEJW\ʜ^EJW\ʜ^ )UNMͬΆ͍ݟͨ໨Ͱॻ͚Δ ˠ֦ுؔ਺Ͱείʔϓ੍ޚ
  9. fun RBuilder.div( classes: String? = null, block: RDOMBuilder<DIV>.() -> Unit

    ): ReactElement = … ,PUMJO+4Ͱ͸ʜ w 3FBDUͷSFOEFSϝιουΛ,PUMJO+4Ͱॻ͘ͱʜ class App : RComponent<RProps, RState>() { override fun RBuilder.render() { div("title") { +"Welcome to React with Kotlin" } } } ϥϜμࣜ Ҿ਺ΛऔΒͳ͍ɺԿ΋ฦ͞ͳ͍ είʔϓ಺ͷUIJT͕ 3%0.#VJMEFSʹͳΔ ˞3%0.#VJMEFSFYUFOET3#VJMEFS
  10. ,PUMJO+4Ͱ͸ʜ w 3FBDUͷSFOEFSϝιουΛ,PUMJO+4Ͱॻ͘ͱʜ class App : RComponent<RProps, RState>() { override

    fun RBuilder.render() { div("title") { +"Welcome to React with Kotlin" } } } ԋࢉࢠΦʔόʔϩʔυ ˠʮEJWλάͰจࣈྻΛڬΉʯ ˠPQFSBUPSGVO4USJOHVOBSZ1MVT  Ͳ͜Ͱ࣮૷͞ΕͯΔͷʁ
  11. w 3FBDUͷSFOEFSϝιουΛ,PUMJO+4Ͱॻ͘ͱʜ open class RBuilder { operator fun String.unaryPlus() {

    childList.add(this) } ... } ,PUMJO+4Ͱ͸ʜ class App : RComponent<RProps, RState>() { override fun RBuilder.render() { div("title") { +"Welcome to React with Kotlin" } } } 3#VJMEFSͷதʹ࣮૷ʂ SFOEFSEJWͷείʔϓ಺ͳΒ  zIPHFzˠDIJME-JTUBEE lIPHFz  ˠϥϜμࣜʹΑΔ είʔϓ੍ޚͷ͓͔͛ ˠԋࢉࢠΦʔόʔϩʔυࠇຐज़ ࢖͑ΔՕॴΛݶఆɺ෭࡞༻⾪
  12. ,PUMJO+4ͱͷ֨ಆͷه࿥ w ஍ࠈ͸͔͜͜Β w +4ͷϥΠϒϥϦʮ.BUFSJBM6*ʯΛ࢖ͬͯ࡞Δͧ w ಥવͷొ৔ɺʮEZOBNJDʯܕ @JsModule("@material-ui/core/Button") private external

    val buttonModule: dynamic @Suppress("UnsafeCastFromDynamic") private val buttonComponent: RComponent<RProps, RState> = buttonModule.default ίϨ
  13. ,PUMJO+4ͱͷ֨ಆͷه࿥ w EZNBOJDܕ,PUMJOͱ+4ͷϒϦοδʹඞਢͳຐ๏ͷܕ w \GPPbGPP` CBS\IPHF^^ͱ͍͏ΦϒδΣΫτͷ৔߹ʜ val jsObject: dynamic =

    js("{ foo: 'foo', bar: { hoge: '111' } }") print(jsObject["foo"]) // => 'foo' print(jsObject["bar"]["hoge"]) // => 111 jsObject["aaa"]["bbb"] = "add from kotlin" print(jsObject["aaa"]["bbb"]) // => add from kotlin ͋ɺ͋Γͷ··ࠓىͬͨ͜͜ͱΛ࿩ͥ͢ʜ ʮΦϨ͸,PUMJOΛॻ͍͍ͯΔͱࢥͬͨΒ +BWBTDSJQUΛॻ͍͍ͯͨʯ ͳɺԿΛݴͬͯΔ͔Θ͔ΒͶʔͱ SZ