SlideShare a Scribd company logo
1 of 28
Download to read offline
Domain Specific Languages in Kotlin
Theory and Practice
Stefan Scheidt, REWE digital
Domain Specific Languages
A domain-specific language (DSL) is a computer language specialized
to a particular application domain.
— Wikipedia
Examples
• HTML
• SQL
• Regular Expressions
• ...
Embedded DSLs
Embedded (or internal) domain-specific languages are implemented as
libraries which exploit the syntax of their host general purpose
language
— Wikipedia
Kotlin DSLs
A domain-specific language embedded in Kotlin
Examples - HTML 1
System.out.appendHTML().html {
body {
div {
a("http://kotlinlang.org") {
target = ATarget.blank
+"Main site"
}
}
}
}
1 
kotlinx.html, https://github.com/Kotlin/kotlinx.html
Examples - JSON 2
import com.github.salomonbrys.kotson.*
val obj: JsonObject = jsonObject(
"property" to "value",
"array" to jsonArray(
21,
"fourty-two",
jsonObject("go" to "hell")
)
)
2 
Kotson, https://github.com/SalomonBrys/Kotson
Examples - Gradle 3
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.2.60"
}
repositories {
mavenCentral()
}
dependencies {
compile(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
3 
Kotlin Gradle DSL, https://github.com/gradle/kotlin-dsl
Examples - Android Layouts 4
verticalLayout {
val name = editText {
hint = "Name"
textSize = 24f
}
button("Say Hello") {
onClick {
toast("Hello, ${name.text}!")
}
}
}
4 
Kotlin Anko Layouts, https://github.com/Kotlin/anko/wiki/Anko-Layouts
Build your own DSL
Ingredients
Extention functions
Instead of this
fun <T> swap(list: MutableList<T>, index1: Int, index2: Int) {
val tmp = list[index1]
list[index1] = list[index2]
list[index2] = tmp
}
val list = mutableListOf(1, 2, 3)
swap(list, 1, 2)
Extention functions
... we can write this
fun <T> MutableList<T>.swap(index1: Int, index2: Int) {
val tmp = this[index1] // 'this' corresponds to the list
this[index1] = this[index2]
this[index2] = tmp
}
val list = mutableListOf(1, 2, 3)
list.swap(1,2)
Lambda Expressions with Receivers
So what does this do?
val printMe: String.() -> Unit = { println(this) }
"Stefan".printMe()
Lambda Expressions with Receivers
Now let's assume we have a class
class HTML {
fun head() {}
fun body() {}
}
and a function
fun html(init: HTML.() -> Unit): HTML {
val html = HTML()
html.init()
return html
}
Lambda Expressions with Receivers
... then we can write
html({
head()
body()
})
Lambda Expressions with Receivers
We apply syntactic sugger:
html() {
head()
body()
}
Lambda Expressions with Receivers
And again:
html {
head()
body()
}
Lambda Expressions with Receivers
Now let's go from here
class HTML {
fun head() {}
fun body() {}
}
Lambda Expressions with Receivers
... to here:
class HTML {
fun head(init: Head.() -> Unit): Head {
val head = Head(); head.init(); return head
}
fun body(init: Body.() -> Unit): Body {
val body = Body(); body.init(); return body
}
}
class Head { fun title() { } }
class Body { fun p() { } }
Lambda Expressions with Receivers
Then we can write
html {
head {
title()
}
body {
p()
}
}
... and so on.
Scope control
But ...
html {
head {
head {
title()
}
}
// ...
}
will also be OK. :-(
DSL Markers
Control receiver scope with @DslMarker:
@DslMarker
annotation class HtmlTagMarker
@HtmlTagMarker
class HTML { // ...
}
@HtmlTagMarker
class Head { // ...
}
@HtmlTagMarker
class Body { // ...
}
DSL Markers
Now we will get
$ ./gradlew compileKotlin
e: .../html.kt: (32, 13): 'fun head(init: Head.() -> Unit):
Head' can't be called in this context by implicit receiver.
Use the explicit one if necessary
> Task :compileKotlin FAILED
DSL Markers
Well, we still could write
html {
head {
this@html.head {
title()
}
}
// ...
}
The whole example could be found here: Type-Safe builders.
Invoke
With
class MyClass {
operator fun invoke() { // ...
}
}
one can write
val myObj = MyClass()
myObj() // will call invoke
Invoke for DSLs
With
class Family {
operator fun invoke(body: Family.() -> Unit) { body() }
fun addMember(name: String) {}
}
one can write
val family = Family()
family {
addMember("Mom"); addMember("Dad"); addMember("Kid")
}
More Examples for DSLs
• https://dzone.com/articles/kotlin-dsl-from-theory-to-practice
convert Test Data Builder to Kotlin DSL
• https://kotlinexpertise.com/java-builders-kotlin-dsls/
convert Java builders for Android Material Drawer to Kotlin DSL
• https://blog.codecentric.de/2018/06/kotlin-dsl-apache-kafka/
A simple example of Kotlin DSL for Apache Kafka producer and
consumer (German)
Thank you!
@stefanscheidt on Twitter and GitHub
This presentation can be found here

More Related Content

What's hot

Redesigning Common Lisp
Redesigning Common LispRedesigning Common Lisp
Redesigning Common Lispfukamachi
 
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]Tom Lee
 
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...GeeksLab Odessa
 
Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Max Kleiner
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and MonoidsHugo Gävert
 
Arduino C maXbox web of things slide show
Arduino C maXbox web of things slide showArduino C maXbox web of things slide show
Arduino C maXbox web of things slide showMax Kleiner
 
Runtime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkESUG
 
LCDS - State Presentation
LCDS - State PresentationLCDS - State Presentation
LCDS - State PresentationRuochun Tzeng
 
EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4Max Kleiner
 
DConf 2016: Bitpacking Like a Madman by Amaury Sechet
DConf 2016: Bitpacking Like a Madman by Amaury SechetDConf 2016: Bitpacking Like a Madman by Amaury Sechet
DConf 2016: Bitpacking Like a Madman by Amaury SechetAndrei Alexandrescu
 
maXbox Starter 39 GEO Maps Tutorial
maXbox Starter 39 GEO Maps TutorialmaXbox Starter 39 GEO Maps Tutorial
maXbox Starter 39 GEO Maps TutorialMax Kleiner
 
Algebird : Abstract Algebra for big data analytics. Devoxx 2014
Algebird : Abstract Algebra for big data analytics. Devoxx 2014Algebird : Abstract Algebra for big data analytics. Devoxx 2014
Algebird : Abstract Algebra for big data analytics. Devoxx 2014Samir Bessalah
 
05 pig user defined functions (udfs)
05 pig user defined functions (udfs)05 pig user defined functions (udfs)
05 pig user defined functions (udfs)Subhas Kumar Ghosh
 
A closure ekon16
A closure ekon16A closure ekon16
A closure ekon16Max Kleiner
 
Metrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMetrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMax Kleiner
 

What's hot (19)

Iron python
Iron pythonIron python
Iron python
 
Kotlin
KotlinKotlin
Kotlin
 
Redesigning Common Lisp
Redesigning Common LispRedesigning Common Lisp
Redesigning Common Lisp
 
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
Open Source Compiler Construction for the JVM [LCA2011 Miniconf]
 
effective_r27
effective_r27effective_r27
effective_r27
 
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
 
Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475Ekon 25 Python4Delphi_MX475
Ekon 25 Python4Delphi_MX475
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
 
Arduino C maXbox web of things slide show
Arduino C maXbox web of things slide showArduino C maXbox web of things slide show
Arduino C maXbox web of things slide show
 
Runtime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for Smalltalk
 
LCDS - State Presentation
LCDS - State PresentationLCDS - State Presentation
LCDS - State Presentation
 
EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4EKON 25 Python4Delphi_mX4
EKON 25 Python4Delphi_mX4
 
DConf 2016: Bitpacking Like a Madman by Amaury Sechet
DConf 2016: Bitpacking Like a Madman by Amaury SechetDConf 2016: Bitpacking Like a Madman by Amaury Sechet
DConf 2016: Bitpacking Like a Madman by Amaury Sechet
 
maXbox Starter 39 GEO Maps Tutorial
maXbox Starter 39 GEO Maps TutorialmaXbox Starter 39 GEO Maps Tutorial
maXbox Starter 39 GEO Maps Tutorial
 
Performance .NET Core - M. Terech, P. Janowski
Performance .NET Core - M. Terech, P. JanowskiPerformance .NET Core - M. Terech, P. Janowski
Performance .NET Core - M. Terech, P. Janowski
 
Algebird : Abstract Algebra for big data analytics. Devoxx 2014
Algebird : Abstract Algebra for big data analytics. Devoxx 2014Algebird : Abstract Algebra for big data analytics. Devoxx 2014
Algebird : Abstract Algebra for big data analytics. Devoxx 2014
 
05 pig user defined functions (udfs)
05 pig user defined functions (udfs)05 pig user defined functions (udfs)
05 pig user defined functions (udfs)
 
A closure ekon16
A closure ekon16A closure ekon16
A closure ekon16
 
Metrics ekon 14_2_kleiner
Metrics ekon 14_2_kleinerMetrics ekon 14_2_kleiner
Metrics ekon 14_2_kleiner
 

Similar to Domain Specific Languages in Kotlin: Theory and Practice

Kotlin boost yourproductivity
Kotlin boost yourproductivityKotlin boost yourproductivity
Kotlin boost yourproductivitynklmish
 
Scala4sling
Scala4slingScala4sling
Scala4slingday
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLsintelliyole
 
Coding for Android on steroids with Kotlin
Coding for Android on steroids with KotlinCoding for Android on steroids with Kotlin
Coding for Android on steroids with KotlinKai Koenig
 
Building a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGLBuilding a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGLLuka Jacobowitz
 
Java script
 Java script Java script
Java scriptbosybosy
 
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)lennartkats
 
Scoobi - Scala for Startups
Scoobi - Scala for StartupsScoobi - Scala for Startups
Scoobi - Scala for Startupsbmlever
 
Creating A Language Editor Using Dltk
Creating A Language Editor Using DltkCreating A Language Editor Using Dltk
Creating A Language Editor Using DltkKaniska Mandal
 
A New Chapter of Data Processing with CDK
A New Chapter of Data Processing with CDKA New Chapter of Data Processing with CDK
A New Chapter of Data Processing with CDKShu-Jeng Hsieh
 
Kotlin- Basic to Advance
Kotlin- Basic to Advance Kotlin- Basic to Advance
Kotlin- Basic to Advance Coder Tech
 
Design patterns with kotlin
Design patterns with kotlinDesign patterns with kotlin
Design patterns with kotlinAlexey Soshin
 
Cs267 hadoop programming
Cs267 hadoop programmingCs267 hadoop programming
Cs267 hadoop programmingKuldeep Dhole
 

Similar to Domain Specific Languages in Kotlin: Theory and Practice (20)

C++ Boot Camp Part 2
C++ Boot Camp Part 2C++ Boot Camp Part 2
C++ Boot Camp Part 2
 
Kotlin boost yourproductivity
Kotlin boost yourproductivityKotlin boost yourproductivity
Kotlin boost yourproductivity
 
Scala4sling
Scala4slingScala4sling
Scala4sling
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
Coding for Android on steroids with Kotlin
Coding for Android on steroids with KotlinCoding for Android on steroids with Kotlin
Coding for Android on steroids with Kotlin
 
Building a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGLBuilding a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGL
 
Introduction to kotlin
Introduction to kotlinIntroduction to kotlin
Introduction to kotlin
 
Java script
 Java script Java script
Java script
 
Power tools in Java
Power tools in JavaPower tools in Java
Power tools in Java
 
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
Domain-Specific Languages for Composable Editor Plugins (LDTA 2009)
 
Scoobi - Scala for Startups
Scoobi - Scala for StartupsScoobi - Scala for Startups
Scoobi - Scala for Startups
 
Tml for Objective C
Tml for Objective CTml for Objective C
Tml for Objective C
 
Creating A Language Editor Using Dltk
Creating A Language Editor Using DltkCreating A Language Editor Using Dltk
Creating A Language Editor Using Dltk
 
A New Chapter of Data Processing with CDK
A New Chapter of Data Processing with CDKA New Chapter of Data Processing with CDK
A New Chapter of Data Processing with CDK
 
Return of c++
Return of c++Return of c++
Return of c++
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
Xtext Webinar
Xtext WebinarXtext Webinar
Xtext Webinar
 
Kotlin- Basic to Advance
Kotlin- Basic to Advance Kotlin- Basic to Advance
Kotlin- Basic to Advance
 
Design patterns with kotlin
Design patterns with kotlinDesign patterns with kotlin
Design patterns with kotlin
 
Cs267 hadoop programming
Cs267 hadoop programmingCs267 hadoop programming
Cs267 hadoop programming
 

More from Stefan Scheidt

Understanding the Four Rules of Simple Design
Understanding the Four Rules of Simple DesignUnderstanding the Four Rules of Simple Design
Understanding the Four Rules of Simple DesignStefan Scheidt
 
iOS Einstieg und Ausblick
iOS Einstieg und AusblickiOS Einstieg und Ausblick
iOS Einstieg und AusblickStefan Scheidt
 
iOS: Einstieg und Ausblick
iOS: Einstieg und AusblickiOS: Einstieg und Ausblick
iOS: Einstieg und AusblickStefan Scheidt
 
Java script data binding mit jQuery Mobile
Java script data binding mit jQuery MobileJava script data binding mit jQuery Mobile
Java script data binding mit jQuery MobileStefan Scheidt
 
Test driven java script development
Test driven java script developmentTest driven java script development
Test driven java script developmentStefan Scheidt
 
Fruehling fuers iPhone
Fruehling fuers iPhoneFruehling fuers iPhone
Fruehling fuers iPhoneStefan Scheidt
 
Automatischer Build mit Maven
Automatischer Build mit MavenAutomatischer Build mit Maven
Automatischer Build mit MavenStefan Scheidt
 
ipdc10: Spring Backends für iOS Apps
ipdc10: Spring Backends für iOS Appsipdc10: Spring Backends für iOS Apps
ipdc10: Spring Backends für iOS AppsStefan Scheidt
 
WJAX 2010: Spring Backends für iOS Apps
WJAX 2010: Spring Backends für iOS AppsWJAX 2010: Spring Backends für iOS Apps
WJAX 2010: Spring Backends für iOS AppsStefan Scheidt
 

More from Stefan Scheidt (10)

Understanding the Four Rules of Simple Design
Understanding the Four Rules of Simple DesignUnderstanding the Four Rules of Simple Design
Understanding the Four Rules of Simple Design
 
iOS Einstieg und Ausblick
iOS Einstieg und AusblickiOS Einstieg und Ausblick
iOS Einstieg und Ausblick
 
iOS: Einstieg und Ausblick
iOS: Einstieg und AusblickiOS: Einstieg und Ausblick
iOS: Einstieg und Ausblick
 
Java script data binding mit jQuery Mobile
Java script data binding mit jQuery MobileJava script data binding mit jQuery Mobile
Java script data binding mit jQuery Mobile
 
Test driven java script development
Test driven java script developmentTest driven java script development
Test driven java script development
 
Fruehling fuers iPhone
Fruehling fuers iPhoneFruehling fuers iPhone
Fruehling fuers iPhone
 
Maven 3 New Features
Maven 3 New FeaturesMaven 3 New Features
Maven 3 New Features
 
Automatischer Build mit Maven
Automatischer Build mit MavenAutomatischer Build mit Maven
Automatischer Build mit Maven
 
ipdc10: Spring Backends für iOS Apps
ipdc10: Spring Backends für iOS Appsipdc10: Spring Backends für iOS Apps
ipdc10: Spring Backends für iOS Apps
 
WJAX 2010: Spring Backends für iOS Apps
WJAX 2010: Spring Backends für iOS AppsWJAX 2010: Spring Backends für iOS Apps
WJAX 2010: Spring Backends für iOS Apps
 

Recently uploaded

What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutionsmonugehlot87
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 

Recently uploaded (20)

What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutions
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 

Domain Specific Languages in Kotlin: Theory and Practice

  • 1. Domain Specific Languages in Kotlin Theory and Practice Stefan Scheidt, REWE digital
  • 2. Domain Specific Languages A domain-specific language (DSL) is a computer language specialized to a particular application domain. — Wikipedia
  • 3. Examples • HTML • SQL • Regular Expressions • ...
  • 4. Embedded DSLs Embedded (or internal) domain-specific languages are implemented as libraries which exploit the syntax of their host general purpose language — Wikipedia
  • 5. Kotlin DSLs A domain-specific language embedded in Kotlin
  • 6. Examples - HTML 1 System.out.appendHTML().html { body { div { a("http://kotlinlang.org") { target = ATarget.blank +"Main site" } } } } 1  kotlinx.html, https://github.com/Kotlin/kotlinx.html
  • 7. Examples - JSON 2 import com.github.salomonbrys.kotson.* val obj: JsonObject = jsonObject( "property" to "value", "array" to jsonArray( 21, "fourty-two", jsonObject("go" to "hell") ) ) 2  Kotson, https://github.com/SalomonBrys/Kotson
  • 8. Examples - Gradle 3 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { kotlin("jvm") version "1.2.60" } repositories { mavenCentral() } dependencies { compile(kotlin("stdlib-jdk8")) } tasks.withType<KotlinCompile> { kotlinOptions.jvmTarget = "1.8" } 3  Kotlin Gradle DSL, https://github.com/gradle/kotlin-dsl
  • 9. Examples - Android Layouts 4 verticalLayout { val name = editText { hint = "Name" textSize = 24f } button("Say Hello") { onClick { toast("Hello, ${name.text}!") } } } 4  Kotlin Anko Layouts, https://github.com/Kotlin/anko/wiki/Anko-Layouts
  • 10. Build your own DSL Ingredients
  • 11. Extention functions Instead of this fun <T> swap(list: MutableList<T>, index1: Int, index2: Int) { val tmp = list[index1] list[index1] = list[index2] list[index2] = tmp } val list = mutableListOf(1, 2, 3) swap(list, 1, 2)
  • 12. Extention functions ... we can write this fun <T> MutableList<T>.swap(index1: Int, index2: Int) { val tmp = this[index1] // 'this' corresponds to the list this[index1] = this[index2] this[index2] = tmp } val list = mutableListOf(1, 2, 3) list.swap(1,2)
  • 13. Lambda Expressions with Receivers So what does this do? val printMe: String.() -> Unit = { println(this) } "Stefan".printMe()
  • 14. Lambda Expressions with Receivers Now let's assume we have a class class HTML { fun head() {} fun body() {} } and a function fun html(init: HTML.() -> Unit): HTML { val html = HTML() html.init() return html }
  • 15. Lambda Expressions with Receivers ... then we can write html({ head() body() })
  • 16. Lambda Expressions with Receivers We apply syntactic sugger: html() { head() body() }
  • 17. Lambda Expressions with Receivers And again: html { head() body() }
  • 18. Lambda Expressions with Receivers Now let's go from here class HTML { fun head() {} fun body() {} }
  • 19. Lambda Expressions with Receivers ... to here: class HTML { fun head(init: Head.() -> Unit): Head { val head = Head(); head.init(); return head } fun body(init: Body.() -> Unit): Body { val body = Body(); body.init(); return body } } class Head { fun title() { } } class Body { fun p() { } }
  • 20. Lambda Expressions with Receivers Then we can write html { head { title() } body { p() } } ... and so on.
  • 21. Scope control But ... html { head { head { title() } } // ... } will also be OK. :-(
  • 22. DSL Markers Control receiver scope with @DslMarker: @DslMarker annotation class HtmlTagMarker @HtmlTagMarker class HTML { // ... } @HtmlTagMarker class Head { // ... } @HtmlTagMarker class Body { // ... }
  • 23. DSL Markers Now we will get $ ./gradlew compileKotlin e: .../html.kt: (32, 13): 'fun head(init: Head.() -> Unit): Head' can't be called in this context by implicit receiver. Use the explicit one if necessary > Task :compileKotlin FAILED
  • 24. DSL Markers Well, we still could write html { head { this@html.head { title() } } // ... } The whole example could be found here: Type-Safe builders.
  • 25. Invoke With class MyClass { operator fun invoke() { // ... } } one can write val myObj = MyClass() myObj() // will call invoke
  • 26. Invoke for DSLs With class Family { operator fun invoke(body: Family.() -> Unit) { body() } fun addMember(name: String) {} } one can write val family = Family() family { addMember("Mom"); addMember("Dad"); addMember("Kid") }
  • 27. More Examples for DSLs • https://dzone.com/articles/kotlin-dsl-from-theory-to-practice convert Test Data Builder to Kotlin DSL • https://kotlinexpertise.com/java-builders-kotlin-dsls/ convert Java builders for Android Material Drawer to Kotlin DSL • https://blog.codecentric.de/2018/06/kotlin-dsl-apache-kafka/ A simple example of Kotlin DSL for Apache Kafka producer and consumer (German)
  • 28. Thank you! @stefanscheidt on Twitter and GitHub This presentation can be found here