Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kotlin で DSL を作り始めるまで
Search
Tomoya Miwa
August 25, 2018
Programming
2
370
Kotlin で DSL を作り始めるまで
Kotlin Fest 2018 での 3分 LT です。
Tomoya Miwa
August 25, 2018
Tweet
Share
More Decks by Tomoya Miwa
See All by Tomoya Miwa
できる!ComposeでCollapsingToolbar
tomoya0x00
0
710
Compose の LazyColumn パフォーマンス改善で取り組んだこと
tomoya0x00
0
1.9k
ComposeのMutableStateってどうやってLocal Unit Testすれば良いの??
tomoya0x00
0
980
意外と簡単?Navigation rail導入のお話
tomoya0x00
0
1.3k
Kotlin Coroutines Flow を触ってみた話し
tomoya0x00
2
740
Android for Carsのお話し
tomoya0x00
1
980
熟成されたアプリのmulti module化(halfway)
tomoya0x00
2
850
コードカバレッジを⾒つつユニットテストを書く
tomoya0x00
0
360
multi module へ向けて
tomoya0x00
0
530
Other Decks in Programming
See All in Programming
あなたのアプリ、ログはでてますか?あるいはログをだしてますか? (Funabashi.dev用 軽量版)
uzulla
2
130
GoのIteratorに詳しくなってしまう
inatonix
1
210
rails_girls_is_my_gate_to_join_the_ruby_commuinty
maimux2x
0
200
GraphQL あるいは React における自律的なデータ取得について
quramy
11
3k
『ドメイン駆動設計をはじめよう』中核の業務領域
masuda220
PRO
5
1k
僕が思い描くTypeScriptの未来を勝手に先取りする
yukukotani
9
2.4k
Kotlin 2.0が与えるAndroid開発の進化
masayukisuda
1
420
Kotlin 2.0 and Beyond
antonarhipov
2
150
開発を加速する共有Swift Package実践
elmetal
PRO
0
420
Jakarta EE meets AI
ivargrimstad
1
550
The Shape of a Service Object
inem
0
530
GraphQLとGigaViewer for Apps
numeroanddev
2
160
Featured
See All Featured
The Illustrated Children's Guide to Kubernetes
chrisshort
47
48k
Gamification - CAS2011
davidbonilla
79
5k
A designer walks into a library…
pauljervisheath
201
24k
How STYLIGHT went responsive
nonsquared
93
5.1k
Art, The Web, and Tiny UX
lynnandtonic
294
20k
The Straight Up "How To Draw Better" Workshop
denniskardys
230
130k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
45
4.8k
YesSQL, Process and Tooling at Scale
rocio
167
14k
Happy Clients
brianwarren
96
6.6k
Fashionably flexible responsive web design (full day workshop)
malarkey
401
65k
Optimizing for Happiness
mojombo
375
69k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
24
610
Transcript
Kotlin で DSL を作り始めるまで tomoya0x00 Kotlin Fest 2018
⾃⼰紹介 tomoya0x00 Twitter, GitHub, Qiita Android, Embedded system, BLE/BT, iOS
DeNAでタクベルつくってます
DSLとは︖
DSLとは︖ Domain Speci c Language の略で特定ドメインに特化した⾔語 HTMLとか、SQLとかもある意味DSL KotlinやJavaなどのプログラミング⾔語に⽐べて汎⽤性は低い 汎⽤性を下げることで、特定分野で使いやすくしている
DSLの何が嬉しいのか 特定ドメインに特化しているので、読みやすい&書きやすい ボイラープレートのような冗⻑な記述を減らせるかも IDEなどでコード補完できる
FSM DSL in Kotlin https://speakerdeck.com/tomoya0x00/fsm-dsl-in-kotlin 以前、 Kotlin Developers Meetup で
LT させて頂いた
例︓FSM DSL
例︓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) } } }
DSL、作ってみたくなりましたか︖
作り始めるまでの流れをご紹介 ⾃分の実体験+α
Step1 DSLを具体的にどのようにして実現するのか 基本を学んでイメージを掴む
Kotlin Koans
Kotlin Koans This tutorial walks you through a series of
exercises to get familiar with Kotlin. https://kotlinlang.org/docs/tutorials/koans.html
Koans の Builders HTMLを⽣成するDSLをつくるお題
DSLの抜粋 html { table { tr (color = getTitleColor()) {
td { text("Product") } td { text("Price") } td { text("Popularity") } } } }
ほとんどHTML
どうやって実現しているのか︖
説明は省略(時間が⾜りない) Koansやってみてください︕︕
次
Creating DSLs in Kotlin JetBrains 社の Hadi Hariri ⽒による講演 https://www.youtube.com/watch?v=TGdAvY5i-sU
もちろん全部英語ですが、 実際にコードを書きながら説明して下さっている ⼀度 Koans の Builders をやっておけば、より理解しやすい ので、英語苦⼿でもなんとかなると思います︕ (YouTubeの⾃動⽣成英語字幕とっても便利)
⾃分が参考になった点 Builder @DslMarker
説明は省略(時間が⾜りない) 講演動画みてください︕︕
Step2 DSLの⽂法を考える
DSLの⽂法を考える 1. DSL⾃体のmustとwantをまとめる 2. 既存のDSLをググって⾒てみる 3. DSLの⽂法案をまとめる
例︓FSM DSLの場合
1. DSL⾃体のmustとwantをまとめる must ⼊れ⼦の状態遷移に対応(最低でも1階層の⼦を持てる) Composite state onEntry/onExit対応
1. DSL⾃体のmustとwantをまとめる want 状態遷移図が吐ける PlantUML記法で出⼒ 宣⾔的に書ける Testable Android Thingsなどでも使える軽量なもの Rxやcoroutine拡張
2. 既存のDSLをググって⾒てみる "fsm dsl kotlin" でググる︕ 取り⼊れたいところ、逆に改善したいところを⾒つける ⼊れ⼦の状態遷移に対応しているのは⾒当たらない 状態名を⽂字列で渡すのはやめておきたい
3. DSLの⽂法案をまとめる // StateとEventはsealed classとして宣⾔ stateMachine { state(StateA, onEntry =
{}, onExit = {}) { edge(EventA, StateC) {/* action */} state(StateB, onEntry = {}, onExit = {}) } }
この後は︖
この後は︖ 1. まずはDSLのコンパイルだけを通す 2. DSLを実際に動かすために必要なデータ構造を考える(FSMでは重要 だった) 3. DSLから 2. で考えたデータ構造への変換処理を書く
4. データ構造を使⽤して実際に動くコードを書く
Thank you!
Kotlinかわいい︕