SlideShare a Scribd company logo
Create your DSL with Kotlin
自己紹介
汪 永興
LINE Fukuoka 株式会社
開発Cチーム所属
Kotlinが大好きです
ワン ヨンシン (Freddie)
Create your DSL with Kotlin
Agenda
・What’s DSL
・Why Kotlin is easy to create DSL
・The ”Setting UI” DSL
Create your DSL with Kotlin
Domain Specific Language
・It is specialized to a particular application domain
Purpose
Create your DSL with Kotlin
Why DSL
• Improve Development Productivity
• Flexible design
• Code readability
Create your DSL with Kotlin
DSL in real world
・SQL
・Regular Expression
・Gradle
Create your DSL with Kotlin
Kotlin DSL in real world
• anko
• kotlintest
• Spring Framework
Why Kotlin is easy to
create DSL
Create your DSL with Kotlin
Main features in Kotlin
To support creating DSL easily
• Extension
• Higher-Order functions
• Lambda with receiver
Create your DSL with Kotlin
Extension
fun Context.getResIdFromAttr(attr: Int): Int {
val value = TypedValue()
theme.resolveAttribute(attr, value, true)
return value.resourceId
}
val resId = context.getResIdFromAttr(R.attr.selectableItemBacktround)
fun Context.getResIdFromAttr(attr: Int): Int {
val value = TypedValue()
theme.resolveAttribute(attr, value, true)
return value.resourceId
}
fun Context.getResIdFromAttr(attr: Int): Int {
val value = TypedValue()
theme.resolveAttribute(attr, value, true)
return value.resourceId
}
Create your DSL with Kotlin
Higher-Order functions
Takes functions as parameter
fun <T> lock(lock: Lock, body: () -> T): T {
lock.lock()
try {
return body()
} finally {
lock.unlock()
}
}
val result = lock (lock, { someResources.operation() } )
Create your DSL with Kotlin
Lambda with receiver
fun buildString(action: StringBuilder.() -> Unit): String {
val sb = StringBuilder()
sb.action()
return sb.toString()
}
buildString {// It is syntax sugar for lambda parameter
this.append(“test”)
this.append(“test”) //’this’ is StringBuilder
}
fun buildString(action: StringBuilder.() -> Unit): String {
val sb = StringBuilder()
sb.action()
return sb.toString()
}
The Setting DSL
Create your DSL with Kotlin
Characteristic of Setting UI
・Structured items
・Variable appearance
Create your DSL with Kotlin
Create your DSL with Kotlin
The syntax
settings {
settingGroup {
settingItem {
…
}
…
}
}
settings {
settingGroup {
settingItem {
…
}
…
}
}
settings {
settingGroup {
settingItem {
…
}
…
}
}
Rule1: settings is the root of the UI layoutRule2: settings includes settingGroupRule3: settingGroup includes settingItem
Create your DSL with Kotlin
Setting
class Settings(ctx: Context) : _ScrollView(ctx) {
internal val rootContainer = verticalLayout().lparams(width = matchParent, height = wrapContent)
fun addGroup(group: SettingGroup) {
rootContainer.addView(group)
}
}
Create your DSL with Kotlin
Rule1
inline fun ViewManager.settings(init: Settings.() -> Unit): Setting {
…
val settings = Settings(ctx)
settings.init()
this.addView(settings)
return settings
}
inline fun ViewManager.settings(init: Settings.() -> Unit): Setting {
…
val settings = Settings(ctx)
settings.init()
this.addView(settings)
return settings
}
inline fun ViewManager.settings(init: Settings.() -> Unit): Setting {
…
val settings = Settings(ctx)
settings.init()
this.addView(settings)
return settings
}
settings { //init()
settingGroup {
settingItem {
…
}
…
}
}
Create your DSL with Kotlin
SettingGroup
class SettingGroup(ctx: Context) : _LinearLayout(ctx) {
var title: String
…
}
Create your DSL with Kotlin
Rule2
inline fun Settings.settingGroup(init: SettingGroup.() -> Unit): SettingGroup {
val settingGroup = SettingGroup(this.context)
settingGroup.init()
…
this.addGroup(settingGroup)
return settingGroup
}
inline fun Settings.settingGroup(init: SettingGroup.() -> Unit): SettingGroup {
val settingGroup = SettingGroup(this.context)
settingGroup.init()
…
this.addGroup(settingGroup)
return settingGroup
}
inline fun Settings.settingGroup(init: SettingGroup.() -> Unit): SettingGroup {
val settingGroup = SettingGroup(this.context)
settingGroup.init()
…
this.addGroup(settingGroup)
return settingGroup
}
Create your DSL with Kotlin
SettingItem
class SettingItem(ctx: Context, …) : _LinearLayout(ctx) {
var leftIconResId: Int?
var rightIconResId: Int?
var title: String
var description: String
…
}
Create your DSL with Kotlin
Rule3
inline fun SettingGroup.settingItem(…, init: SettingItem.() -> Unit): SettingItem {
…
val settingItem = SettingItem(ctx, …)
settingItem.init()
this.addView(settingItem)
return settingItem
}
inline fun SettingGroup.settingItem(…, init: SettingItem.() -> Unit): SettingItem {
…
val settingItem = SettingItem(ctx, …)
settingItem.init()
this.addView(settingItem)
return settingItem
}
inline fun SettingGroup.settingItem(…, init: SettingItem.() -> Unit): SettingItem {
…
val settingItem = SettingItem(ctx, …)
settingItem.init()
this.addView(settingItem)
return settingItem
}
Create your DSL with Kotlin
DEMO Time!
https://github.com/wangyung/android-setting-ui-dsl
Create your DSL with Kotlin
Summary
・It is very easy to create custom DSL with Kotlin
・We can use DSL to write readable, maintainable code very easily
Q&A

More Related Content

PDF
Seoul dedicated server
PDF
No sql database and python -- Presentation done at Python Developer's meetup ...
PDF
Learn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe
PDF
Kotlin talk
PDF
Kotlin: Why Do You Care?
PDF
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
PDF
Koin Quickstart
PDF
The Ring programming language version 1.5.4 book - Part 15 of 185
Seoul dedicated server
No sql database and python -- Presentation done at Python Developer's meetup ...
Learn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe
Kotlin talk
Kotlin: Why Do You Care?
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Koin Quickstart
The Ring programming language version 1.5.4 book - Part 15 of 185

Similar to Create your DSL with Kotlin (20)

PPTX
Entity Framework: Code First and Magic Unicorns
PPTX
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
PPTX
A TypeScript Fans KotlinJS Adventures
PDF
The Ring programming language version 1.3 book - Part 8 of 88
PPTX
TypeScript Vs. KotlinJS
PDF
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
PDF
Container orchestration k8s azure kubernetes services
PPTX
GDG Kuwait - Modern android development
PDF
Introduction to Kotlin - Android KTX
PDF
Kotlin The Whole Damn Family
KEY
Simple Build Tool Introduction
PDF
Bye bye $GLOBALS['TYPO3_DB']
PDF
Apache DeltaSpike the CDI toolbox
PDF
Apache DeltaSpike: The CDI Toolbox
PDF
Kickstarting Kotlin Culture, Part 1 - Neil Power
PDF
Structuring Spark: DataFrames, Datasets, and Streaming by Michael Armbrust
PDF
Go Faster With Native Compilation
PDF
Whats new in .NET for 2019
PPTX
Kotlin / Android Update
PPTX
Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Code First and Magic Unicorns
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
A TypeScript Fans KotlinJS Adventures
The Ring programming language version 1.3 book - Part 8 of 88
TypeScript Vs. KotlinJS
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Container orchestration k8s azure kubernetes services
GDG Kuwait - Modern android development
Introduction to Kotlin - Android KTX
Kotlin The Whole Damn Family
Simple Build Tool Introduction
Bye bye $GLOBALS['TYPO3_DB']
Apache DeltaSpike the CDI toolbox
Apache DeltaSpike: The CDI Toolbox
Kickstarting Kotlin Culture, Part 1 - Neil Power
Structuring Spark: DataFrames, Datasets, and Streaming by Michael Armbrust
Go Faster With Native Compilation
Whats new in .NET for 2019
Kotlin / Android Update
Entity Framework: Nakov @ BFU Hackhaton 2015
Ad

More from LINE Corporation (20)

PDF
JJUG CCC 2018 Fall 懇親会LT
PDF
Reduce dependency on Rx with Kotlin Coroutines
PDF
Kotlin/NativeでAndroidのNativeメソッドを実装してみた
PDF
Use Kotlin scripts and Clova SDK to build your Clova extension
PDF
The Magic of LINE 購物 Testing
PPTX
GA Test Automation
PDF
UI Automation Test with JUnit5
PDF
Feature Detection for UI Testing
PDF
LINE 新星計劃介紹與新創團隊分享
PDF
​LINE 技術合作夥伴與應用分享
PDF
LINE 開發者社群經營與技術推廣
PDF
日本開發者大會短講分享
PDF
LINE Chatbot - 活動報名報到設計分享
PDF
在 LINE 私有雲中使用 Managed Kubernetes
PDF
LINE TODAY高效率的敏捷測試開發技巧
PDF
LINE 區塊鏈平台及代幣經濟 - LINK Chain及LINK介紹
PDF
LINE Things - LINE IoT平台新技術分享
PDF
LINE Pay - 一卡通支付新體驗
PDF
LINE Platform API Update - 打造一個更好的Chatbot服務
PDF
Keynote - ​LINE 的技術策略佈局與跨國產品開發
JJUG CCC 2018 Fall 懇親会LT
Reduce dependency on Rx with Kotlin Coroutines
Kotlin/NativeでAndroidのNativeメソッドを実装してみた
Use Kotlin scripts and Clova SDK to build your Clova extension
The Magic of LINE 購物 Testing
GA Test Automation
UI Automation Test with JUnit5
Feature Detection for UI Testing
LINE 新星計劃介紹與新創團隊分享
​LINE 技術合作夥伴與應用分享
LINE 開發者社群經營與技術推廣
日本開發者大會短講分享
LINE Chatbot - 活動報名報到設計分享
在 LINE 私有雲中使用 Managed Kubernetes
LINE TODAY高效率的敏捷測試開發技巧
LINE 區塊鏈平台及代幣經濟 - LINK Chain及LINK介紹
LINE Things - LINE IoT平台新技術分享
LINE Pay - 一卡通支付新體驗
LINE Platform API Update - 打造一個更好的Chatbot服務
Keynote - ​LINE 的技術策略佈局與跨國產品開發
Ad

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
Teaching material agriculture food technology
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
A Presentation on Artificial Intelligence
Per capita expenditure prediction using model stacking based on satellite ima...
A comparative analysis of optical character recognition models for extracting...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Reach Out and Touch Someone: Haptics and Empathic Computing
MIND Revenue Release Quarter 2 2025 Press Release
Digital-Transformation-Roadmap-for-Companies.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Machine learning based COVID-19 study performance prediction
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Network Security Unit 5.pdf for BCA BBA.
“AI and Expert System Decision Support & Business Intelligence Systems”
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Teaching material agriculture food technology
Empathic Computing: Creating Shared Understanding
Big Data Technologies - Introduction.pptx
Spectroscopy.pptx food analysis technology
A Presentation on Artificial Intelligence

Create your DSL with Kotlin

  • 1. Create your DSL with Kotlin
  • 2. 自己紹介 汪 永興 LINE Fukuoka 株式会社 開発Cチーム所属 Kotlinが大好きです ワン ヨンシン (Freddie)
  • 3. Create your DSL with Kotlin Agenda ・What’s DSL ・Why Kotlin is easy to create DSL ・The ”Setting UI” DSL
  • 4. Create your DSL with Kotlin Domain Specific Language ・It is specialized to a particular application domain Purpose
  • 5. Create your DSL with Kotlin Why DSL • Improve Development Productivity • Flexible design • Code readability
  • 6. Create your DSL with Kotlin DSL in real world ・SQL ・Regular Expression ・Gradle
  • 7. Create your DSL with Kotlin Kotlin DSL in real world • anko • kotlintest • Spring Framework
  • 8. Why Kotlin is easy to create DSL
  • 9. Create your DSL with Kotlin Main features in Kotlin To support creating DSL easily • Extension • Higher-Order functions • Lambda with receiver
  • 10. Create your DSL with Kotlin Extension fun Context.getResIdFromAttr(attr: Int): Int { val value = TypedValue() theme.resolveAttribute(attr, value, true) return value.resourceId } val resId = context.getResIdFromAttr(R.attr.selectableItemBacktround) fun Context.getResIdFromAttr(attr: Int): Int { val value = TypedValue() theme.resolveAttribute(attr, value, true) return value.resourceId } fun Context.getResIdFromAttr(attr: Int): Int { val value = TypedValue() theme.resolveAttribute(attr, value, true) return value.resourceId }
  • 11. Create your DSL with Kotlin Higher-Order functions Takes functions as parameter fun <T> lock(lock: Lock, body: () -> T): T { lock.lock() try { return body() } finally { lock.unlock() } } val result = lock (lock, { someResources.operation() } )
  • 12. Create your DSL with Kotlin Lambda with receiver fun buildString(action: StringBuilder.() -> Unit): String { val sb = StringBuilder() sb.action() return sb.toString() } buildString {// It is syntax sugar for lambda parameter this.append(“test”) this.append(“test”) //’this’ is StringBuilder } fun buildString(action: StringBuilder.() -> Unit): String { val sb = StringBuilder() sb.action() return sb.toString() }
  • 14. Create your DSL with Kotlin Characteristic of Setting UI ・Structured items ・Variable appearance
  • 15. Create your DSL with Kotlin
  • 16. Create your DSL with Kotlin The syntax settings { settingGroup { settingItem { … } … } } settings { settingGroup { settingItem { … } … } } settings { settingGroup { settingItem { … } … } } Rule1: settings is the root of the UI layoutRule2: settings includes settingGroupRule3: settingGroup includes settingItem
  • 17. Create your DSL with Kotlin Setting class Settings(ctx: Context) : _ScrollView(ctx) { internal val rootContainer = verticalLayout().lparams(width = matchParent, height = wrapContent) fun addGroup(group: SettingGroup) { rootContainer.addView(group) } }
  • 18. Create your DSL with Kotlin Rule1 inline fun ViewManager.settings(init: Settings.() -> Unit): Setting { … val settings = Settings(ctx) settings.init() this.addView(settings) return settings } inline fun ViewManager.settings(init: Settings.() -> Unit): Setting { … val settings = Settings(ctx) settings.init() this.addView(settings) return settings } inline fun ViewManager.settings(init: Settings.() -> Unit): Setting { … val settings = Settings(ctx) settings.init() this.addView(settings) return settings } settings { //init() settingGroup { settingItem { … } … } }
  • 19. Create your DSL with Kotlin SettingGroup class SettingGroup(ctx: Context) : _LinearLayout(ctx) { var title: String … }
  • 20. Create your DSL with Kotlin Rule2 inline fun Settings.settingGroup(init: SettingGroup.() -> Unit): SettingGroup { val settingGroup = SettingGroup(this.context) settingGroup.init() … this.addGroup(settingGroup) return settingGroup } inline fun Settings.settingGroup(init: SettingGroup.() -> Unit): SettingGroup { val settingGroup = SettingGroup(this.context) settingGroup.init() … this.addGroup(settingGroup) return settingGroup } inline fun Settings.settingGroup(init: SettingGroup.() -> Unit): SettingGroup { val settingGroup = SettingGroup(this.context) settingGroup.init() … this.addGroup(settingGroup) return settingGroup }
  • 21. Create your DSL with Kotlin SettingItem class SettingItem(ctx: Context, …) : _LinearLayout(ctx) { var leftIconResId: Int? var rightIconResId: Int? var title: String var description: String … }
  • 22. Create your DSL with Kotlin Rule3 inline fun SettingGroup.settingItem(…, init: SettingItem.() -> Unit): SettingItem { … val settingItem = SettingItem(ctx, …) settingItem.init() this.addView(settingItem) return settingItem } inline fun SettingGroup.settingItem(…, init: SettingItem.() -> Unit): SettingItem { … val settingItem = SettingItem(ctx, …) settingItem.init() this.addView(settingItem) return settingItem } inline fun SettingGroup.settingItem(…, init: SettingItem.() -> Unit): SettingItem { … val settingItem = SettingItem(ctx, …) settingItem.init() this.addView(settingItem) return settingItem }
  • 23. Create your DSL with Kotlin DEMO Time! https://github.com/wangyung/android-setting-ui-dsl
  • 24. Create your DSL with Kotlin Summary ・It is very easy to create custom DSL with Kotlin ・We can use DSL to write readable, maintainable code very easily
  • 25. Q&A

Editor's Notes

  • #3: 簡単な挨拶 みなさんこんばんは。 私はフレディです。 よろしくお願いします! 今日のTopicはKotlinで 自分のDSLを作るっていうことです。
  • #4: まず簡単に自己紹介させていただきます。 私の名前は汪永興、 nicknameは Freddie それからみんながよくフレディと呼ばれています。 二年間にAndroid engineerとしてLINE Fukuokaで働いていて、来週からLINE京都に転勤します。
  • #5: before start Kotlinを聞いたことがある方はいらしゃいますか? Kotlinを使っている方 KotlinでDSLを作った方 Agenda: 1. DSLとはなにか、 2. なぜコトリンはDSLを作りやすいか、 3. 最後にUIセッティングのDSLについてです
  • #6: 今から英語を使います…w
  • #7: 使うのか? 効率よくする 読みやすくする 
  • #9: anko: For android UI layout kotlintest: For writing unit tests Spring Framework: Bean creation, routing
  • #10: 説明します
  • #14: Talk about the syntax sugar