SlideShare a Scribd company logo
Kotlin DSL
Building
@antonarhipov
@antonarhipov
Why Kotlin?
Null-safety
Coroutines
Multiplatform
Syntax
Why Kotlin?
Null-safety
Coroutines
Multiplatform
Syntax
Why Kotlin?
Null-safety
Coroutines
Multiplatform
Syntax
Why Kotlin?
Null-safety
Coroutines
Multiplatform
Syntax
Why Kotlin?
Null-safety
Coroutines
Multiplatform
Syntax
Why Kotlin?
DSL
“Domain-speci
fi
c”, i.e.
tailored for a
speci
fi
c task
“Domain-speci
fi
c”, i.e.
External
External VS Internal
External VS Internal
External VS Internal
External VS Internal
External VS Internal
Internal
JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdf
+
JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdf
They all
 

look
 

similar!
foo {


bar {


baz = "Hello!"


qux = quux {


corge = "Blah"


}


}


}
foo {


bar {


baz = "Hello!"


qux = quux {


corge = "Blah"


}


}


}
foo {


bar {


baz = "Hello!"


qux = quux {


corge = "Blah"


}


}


}
foo {


bar {


baz = "Hello!"


qux = quux {


corge = "Blah"


}


}


}
foo {


bar(grault = 1) {


baz = "Hello!"


qux = quux {


corge = "Blah"


}


}


}
foo {


bar(grault = 1) {


baz = "Hello!"


qux = quux {


corge = Blah()


}


}


}
foo {


bar(grault = 1) {


baz = "Hello!"


qux = quux {


corge = Blah()


}


}


}
Let’s write
 

some code!
https://github.com/antonarhipov/kotlin-dsl-examples
Let’s write
 

some code!
https://github.com/antonarhipov/kotlin-dsl-examples
Type-safe builders
https://kotlinlang.org/docs/reference/type-safe-builders.html
final ClientBuilder builder = new ClientBuilder();


builder.setFirstName("Anton");


builder.setLastName("Arhipov");


final TwitterBuilder twitterBuilder = new TwitterBuilder();


twitterBuilder.setHandle("@antonarhipov");


builder.setTwitter(twitterBuilder.build());


final CompanyBuilder companyBuilder = new CompanyBuilder();


companyBuilder.setName("JetBrains");


companyBuilder.setCity("Tallinn");


builder.setCompany(companyBuilder.build());


final Client client = builder.build();


System.out.println("Created client is: " + client);


val client = createClient {


firstName = "Anton"


lastName = "Arhipov"


twitter {


handle = "@antonarhipov"


}


company {


name = "JetBrains"


city = "Tallinn"


}


}


println("Created client is: " + client.consoleString)
//extension property


val Client.consoleString: String


get() = "${twitter.handle} ${company.name}"


//extension method


fun Client.toConsoleString(): String {


return "${twitter.handle} ${company.name}"


}
fun createClient(c: ClientBuilder.() -> Unit): Client {


val builder = ClientBuilder()


c(builder)


return builder.build()


}


fun ClientBuilder.company(t: CompanyBuilder.() -> Unit) {


company = CompanyBuilder().apply(t).build()


}


fun ClientBuilder.twitter(t: CompanyBuilder.() -> Unit) {


twitter = TwitterBuilder().apply(t).build()


}


Lambda with receiver
Extension methods
In
fi
x notation
https://kotlinlang.org/docs/reference/functions.html
dateTime = LocalDateTime.of(2018, Month.DECEMBER, 11, 0, 0)
dateTime = 11 December 2018 at (14 hh 0)
infix fun Int.December(n: Int) : LocalDate {


return LocalDate.of(n, Month.DECEMBER, this)


}


infix fun LocalDate.at(n: Pair<Int, Int>): LocalDateTime {


return this.atTime(n.first, n.second)


}


infix fun Int.hh(n: Int): Pair<Int, Int> {


return Pair(this, n)


}
Context Receivers
KEEP-259
Experimental
context(DateContext)


@DateDsl


infix fun Int.March(n: Int): LD = LD.of(n, Month.MARCH, this)
class DateContext
val client = createClient {


/
/
.
.
.


dob = 24 March 2000


}


24 March 2000
fun client(block: context(DateContext) ClientBuilder.()
-
>
Unit): Client {


val builder = ClientBuilder()


with(DateContext()) {


block(builder)


}


return builder.build()


}


Context object
Context requirement
Context requirement
Context scope
The function is available in
the context scope
The function is not available,
missing DateContext
T.() -> Unit
Kotlin {


YouTube = "youtube.com/kotlin"


Slack = "slack.kotl.in"


}
me {


name = "Anton Arhipov"


twitter = "@antonarhipov"


slides = speakerdeck.com/antonarhipov


}
books { courses {

More Related Content

PDF
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
PDF
GeeCON Prague 2018 - Kotlin DSL in under an hour
PPTX
Kotlin / Android Update
PDF
The Ring programming language version 1.5.2 book - Part 45 of 181
PDF
The Ring programming language version 1.10 book - Part 56 of 212
PDF
Type safe embedded domain-specific languages
PDF
«Продакшн в Kotlin DSL» Сергей Рыбалкин
PDF
Consuming web services asynchronously with Futures and Rx Observables (svcc, ...
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
GeeCON Prague 2018 - Kotlin DSL in under an hour
Kotlin / Android Update
The Ring programming language version 1.5.2 book - Part 45 of 181
The Ring programming language version 1.10 book - Part 56 of 212
Type safe embedded domain-specific languages
«Продакшн в Kotlin DSL» Сергей Рыбалкин
Consuming web services asynchronously with Futures and Rx Observables (svcc, ...

Similar to JavaZone 2022 - Building Kotlin DSL.pdf (20)

PDF
Coding for Android on steroids with Kotlin
PDF
The Ring programming language version 1.5.3 book - Part 15 of 184
PPTX
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
PDF
Kotlin, smarter development for the jvm
PDF
Hashiconf EU 2019 - A Tour of Terraform 0.12
PDF
Practical tips for building apps with kotlin
PDF
Save time with kotlin in android development
PDF
The Ring programming language version 1.8 book - Part 19 of 202
PDF
CoreOS + Kubernetes @ All Things Open 2015
PDF
The Ring programming language version 1.9 book - Part 21 of 210
PDF
Advanced #2 networking
PDF
Kotlin boost yourproductivity
PDF
The Ring programming language version 1.10 book - Part 22 of 212
PPTX
GDG Kuwait - Modern android development
PPTX
Kotlin
PPTX
Introducing coServ
PPTX
KotlinForJavaDevelopers-UJUG.pptx
PDF
From Java to Kotlin - The first month in practice
PPT
iOS Development with Blocks
PPTX
CoreOS in a Nutshell
Coding for Android on steroids with Kotlin
The Ring programming language version 1.5.3 book - Part 15 of 184
Beginning direct3d gameprogrammingcpp02_20160324_jintaeks
Kotlin, smarter development for the jvm
Hashiconf EU 2019 - A Tour of Terraform 0.12
Practical tips for building apps with kotlin
Save time with kotlin in android development
The Ring programming language version 1.8 book - Part 19 of 202
CoreOS + Kubernetes @ All Things Open 2015
The Ring programming language version 1.9 book - Part 21 of 210
Advanced #2 networking
Kotlin boost yourproductivity
The Ring programming language version 1.10 book - Part 22 of 212
GDG Kuwait - Modern android development
Kotlin
Introducing coServ
KotlinForJavaDevelopers-UJUG.pptx
From Java to Kotlin - The first month in practice
iOS Development with Blocks
CoreOS in a Nutshell
Ad

More from Anton Arhipov (20)

PDF
Idiomatic kotlin
PDF
TechTrain 2019 - (Не)адекватное техническое интервью
PDF
Build pipelines with TeamCity
PDF
Build pipelines with TeamCity
PDF
Build pipelines with TeamCity and Kotlin DSL
PDF
Build pipelines with TeamCity
PDF
JavaDay Kiev 2017 - Integration testing with TestContainers
PDF
GeeCON Prague 2017 - TestContainers
PDF
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
PDF
JavaOne 2017 - TestContainers: integration testing without the hassle
PDF
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
PDF
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
PDF
JUG.ua 20170225 - Java bytecode instrumentation
PDF
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
PDF
GeeCON 2017 - TestContainers. Integration testing without the hassle
PDF
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
PDF
JEEConf 2017 - Having fun with Javassist
PDF
Devclub 01/2017 - (Не)адекватное Java-интервью
PDF
Joker 2016 - Bytecode 101
PDF
JPoint 2016 - Etudes of DIY Java profiler
Idiomatic kotlin
TechTrain 2019 - (Не)адекватное техническое интервью
Build pipelines with TeamCity
Build pipelines with TeamCity
Build pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity
JavaDay Kiev 2017 - Integration testing with TestContainers
GeeCON Prague 2017 - TestContainers
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JUG.ua 20170225 - Java bytecode instrumentation
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
GeeCON 2017 - TestContainers. Integration testing without the hassle
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - Having fun with Javassist
Devclub 01/2017 - (Не)адекватное Java-интервью
Joker 2016 - Bytecode 101
JPoint 2016 - Etudes of DIY Java profiler
Ad

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
1. Introduction to Computer Programming.pptx
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PPTX
Tartificialntelligence_presentation.pptx
PDF
Approach and Philosophy of On baking technology
PDF
August Patch Tuesday
PPTX
Programs and apps: productivity, graphics, security and other tools
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Digital-Transformation-Roadmap-for-Companies.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
MIND Revenue Release Quarter 2 2025 Press Release
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation_ Review paper, used for researhc scholars
1. Introduction to Computer Programming.pptx
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Mobile App Security Testing_ A Comprehensive Guide.pdf
A comparative analysis of optical character recognition models for extracting...
TLE Review Electricity (Electricity).pptx
Machine learning based COVID-19 study performance prediction
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Univ-Connecticut-ChatGPT-Presentaion.pdf
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Tartificialntelligence_presentation.pptx
Approach and Philosophy of On baking technology
August Patch Tuesday
Programs and apps: productivity, graphics, security and other tools

JavaZone 2022 - Building Kotlin DSL.pdf