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

Kotlin で DSL を作り始めるまで

Kotlin で DSL を作り始めるまで

Kotlin Fest 2018 での 3分 LT です。

Tomoya Miwa

August 25, 2018
Tweet

More Decks by Tomoya Miwa

Other Decks in Programming

Transcript

  1. 例︓FSM DSL val sm = stateMachine(initial = MyState.NotLoaned) { state(MyState.NotLoaned)

    { edge(MyEvent.PressRental, next = MyState.Lock) } state(MyState.OnLoan, entry = { println("turnOnRentalLed") }, exit = { println("turnOffRentalLed") }) { state(MyState.Lock) { edge(MyEvent.PressReturn, next = MyState.NotLoaned) edge(MyEvent.PressUnLock, next = MyState.UnLock) } state(MyState.UnLock) { edge(MyEvent.PressLock, next = MyState.Lock) } } }
  2. Kotlin Koans This tutorial walks you through a series of

    exercises to get familiar with Kotlin. https://kotlinlang.org/docs/tutorials/koans.html
  3. DSLの抜粋 html { table { tr (color = getTitleColor()) {

    td { text("Product") } td { text("Price") } td { text("Popularity") } } } }
  4. Creating DSLs in Kotlin JetBrains 社の Hadi Hariri ⽒による講演 https://www.youtube.com/watch?v=TGdAvY5i-sU

    もちろん全部英語ですが、 実際にコードを書きながら説明して下さっている ⼀度 Koans の Builders をやっておけば、より理解しやすい ので、英語苦⼿でもなんとかなると思います︕ (YouTubeの⾃動⽣成英語字幕とっても便利)
  5. 3. DSLの⽂法案をまとめる // StateとEventはsealed classとして宣⾔ stateMachine { state(StateA, onEntry =

    {}, onExit = {}) { edge(EventA, StateC) {/* action */} state(StateB, onEntry = {}, onExit = {}) } }