SlideShare a Scribd company logo
1 of 25
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

Similar to Create your DSL with Kotlin

Entity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsEntity Framework: Code First and Magic Unicorns
Entity Framework: Code First and Magic UnicornsRichie Rump
 
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...dotNet Miami
 
A TypeScript Fans KotlinJS Adventures
A TypeScript Fans KotlinJS AdventuresA TypeScript Fans KotlinJS Adventures
A TypeScript Fans KotlinJS AdventuresGarth Gilmour
 
The Ring programming language version 1.3 book - Part 8 of 88
The Ring programming language version 1.3 book - Part 8 of 88The Ring programming language version 1.3 book - Part 8 of 88
The Ring programming language version 1.3 book - Part 8 of 88Mahmoud Samir Fayed
 
TypeScript Vs. KotlinJS
TypeScript Vs. KotlinJSTypeScript Vs. KotlinJS
TypeScript Vs. KotlinJSGarth Gilmour
 
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...
Cassandra Summit 2014: Highly Scalable Web Application in the Cloud with Cass...DataStax Academy
 
Container orchestration k8s azure kubernetes services
Container orchestration  k8s azure kubernetes servicesContainer orchestration  k8s azure kubernetes services
Container orchestration k8s azure kubernetes servicesRajesh Kolla
 
GDG Kuwait - Modern android development
GDG Kuwait - Modern android developmentGDG Kuwait - Modern android development
GDG Kuwait - Modern android developmentGDGKuwaitGoogleDevel
 
Kotlin The Whole Damn Family
Kotlin The Whole Damn FamilyKotlin The Whole Damn Family
Kotlin The Whole Damn FamilyGarth Gilmour
 
Bye bye $GLOBALS['TYPO3_DB']
Bye bye $GLOBALS['TYPO3_DB']Bye bye $GLOBALS['TYPO3_DB']
Bye bye $GLOBALS['TYPO3_DB']Jan Helke
 
Kickstarting Kotlin Culture, Part 1 - Neil Power
Kickstarting Kotlin Culture, Part 1 - Neil PowerKickstarting Kotlin Culture, Part 1 - Neil Power
Kickstarting Kotlin Culture, Part 1 - Neil PowerNeil Power
 
Structuring Spark: DataFrames, Datasets, and Streaming by Michael Armbrust
Structuring Spark: DataFrames, Datasets, and Streaming by Michael ArmbrustStructuring Spark: DataFrames, Datasets, and Streaming by Michael Armbrust
Structuring Spark: DataFrames, Datasets, and Streaming by Michael ArmbrustSpark Summit
 
Whats new in .NET for 2019
Whats new in .NET for 2019Whats new in .NET for 2019
Whats new in .NET for 2019Rory Preddy
 
Kotlin / Android Update
Kotlin / Android UpdateKotlin / Android Update
Kotlin / Android UpdateGarth Gilmour
 
Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Entity Framework: Nakov @ BFU Hackhaton 2015
Entity Framework: Nakov @ BFU Hackhaton 2015Svetlin Nakov
 

Similar to Create your DSL with Kotlin (20)

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

More from LINE Corporation

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

More from LINE Corporation (20)

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

Recently uploaded

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 

Recently uploaded (20)

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 

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

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