SlideShare a Scribd company logo
Kotlin DSL

in under an hour
@antonarhipov
Prague, 18-19 October 2018
DSLanother
Yet
talk
DSL wat??
External DSL
Internal DSL
createHTML().html {
head {
+"Hello!"
}
body {
ul {
p {
+"This is my awesome text!"
}
}
}
}
kotlinx.html
https://github.com/Kotlin/kotlinx.html
Anko
https://github.com/Kotlin/anko
verticalLayout {
padding = dip(30)
val name = editText {
hint = "Name"
textSize = 24f
}
val pwd = editText {
hint = "Password"
textSize = 24f
button("Login") {
textSize = 26f
onClick { toast("Hello, ${name.text}!") }
}
Gradle Kotlin DSL
https://github.com/gradle/kotlin-dsl
plugins {
java
kotlin("jvm") version "1.2.70"
}
group = "org.arhan"
version = "1.0-SNAPSHOT"
repositories {
maven { setUrl("http://dl.bintray.com/kotlin/kotlin-eap") }
jcenter()
}
dependencies {
val kotlinx_html_version = "0.6.11"
compile(kotlin("stdlib-jdk8"))
compile("org.jetbrains.kotlinx:kotlinx-html-jvm:${kotlinx_html_version}")
testCompile("junit", "junit", "4.12")
Exposed
https://github.com/JetBrains/Exposed
object Cities : Table() {
val id = integer("id").autoIncrement().primaryKey() // Column<Int>
val name = varchar("name", 50) // Column<String>
}
transaction {
create (Cities)
val tallinnId = Cities.insert {
it[name] = "Tallinn"
} get Cities.id
for (city in Cities.selectAll()) {
println("${city[Cities.id]}: ${city[Cities.name]}")
}
}
https://github.com/nfrankel/kaadin
Kaadin
theme = "valo"
verticalLayout(margin = true, spacing = true) {
tabSheet {
tab("Interactions") {
accordion {
tab("Button", HAND_O_RIGHT) {
horizontalLayout(true, true) {
button()
button("Label")
button("Label", HAND_O_RIGHT)
button("Click me", onClick = { show("Clicked") })
button("Click me", HAND_O_RIGHT, { show("Clicked") })
}
}
k8s-kotlin-dsl
https://github.com/fkorotkov/k8s-kotlin-dsl
val client = DefaultKubernetesClient().inNamespace("default")
client.extensions().ingresses().createOrReplace(
newIngress {
metadata {
name = "example-ingress"
}
spec {
backend {
serviceName = "example-service"
servicePort = IntOrString(8080)
}
}
}
)
Kotlin DSL in TeamCity
project {
vcsRoot(ApplicationVcs)
buildType {
id("Application")
name = "Application"
vcs { root(ApplicationVcs) }
artifactRules = "target/*jar"
steps {
maven { goals = "clean package" }
}
triggers { vcs {} }
dependencies {
snapshot(Library) {}
fun main(args: Array<String>) {
embeddedServer(Jetty, commandLineEnvironment(args)).start(wait = true)
}
fun Application.main() {
install(DefaultHeaders)
install(CallLogging)
routing {
get("/") {
call.respondHtml {
head {
title { +"Ktor App" }
}
body {
p {
+"Hello from Ktor!"
}
}
Ktor
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/jonnyzzz/kotlin-dsl-demo
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
twitter {
handle = "@antonarhipov"
company {
name = "JetBrains"
city = "Tallinn"
}
}
@DslMarker
annotation class ClientDsl
@ClientDsl
class CompanyBuilderDsl : CompanyBuilder()
@ClientDsl
class TwitterBuilderDsl : TwitterBuilder()
twitter {
handle = "@antonarhipov"
company {
name = "JetBrains"
city = "Tallinn"
}
}
Scope control
Infix 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)
}
T.() -> Unit
me {
name = "Anton Arhipov"
twitter = "@antonarhipov"
}
books { courses {
Kotlin {
Try = "try.kotlinlang.org"
Slack = "slack.kotl.in"
}

More Related Content

PDF
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
PPTX
CouchDB Day NYC 2017: Replication
PDF
Groovy Powered Clean Code
PDF
HTTPBuilder NG: Back From The Dead
PPTX
CouchDB Day NYC 2017: Introduction to CouchDB 2.0
PPTX
CouchDB Day NYC 2017: Core HTTP API
PDF
Contributing to an os project
PDF
リバースプロキシで webサーバを集約 ついでにdocker化しよう
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
CouchDB Day NYC 2017: Replication
Groovy Powered Clean Code
HTTPBuilder NG: Back From The Dead
CouchDB Day NYC 2017: Introduction to CouchDB 2.0
CouchDB Day NYC 2017: Core HTTP API
Contributing to an os project
リバースプロキシで webサーバを集約 ついでにdocker化しよう

What's hot (20)

PPTX
Coroutines talk ppt
KEY
The story and tech of Read the Docs
PDF
Git Tutorial Yang Yang
PDF
Linux containers
PDF
git & GitHub workshop
PDF
Virthualenvwrapper
PDF
Coroutines for Kotlin Multiplatform in Practise
PDF
Python と Docker で mypy Playground を開発した話
PDF
Kotlin wonderland
PDF
運用 Exposed 管理及操作資料庫
PDF
Spark Day 2017- Spark 의 과거, 현재, 미래
PPTX
Ride the Snake: reddit keynote @ PyCon 09
PDF
Android Libs - Retrofit
PDF
Kotlin for API (with Ktor)
PDF
Draw More, Work Less
PPTX
PlantUML
PPTX
PDF
Greach - The Groovy Ecosystem
PDF
はじめてのUnitTest XCTestに触れて
KEY
Git and GitHub
Coroutines talk ppt
The story and tech of Read the Docs
Git Tutorial Yang Yang
Linux containers
git & GitHub workshop
Virthualenvwrapper
Coroutines for Kotlin Multiplatform in Practise
Python と Docker で mypy Playground を開発した話
Kotlin wonderland
運用 Exposed 管理及操作資料庫
Spark Day 2017- Spark 의 과거, 현재, 미래
Ride the Snake: reddit keynote @ PyCon 09
Android Libs - Retrofit
Kotlin for API (with Ktor)
Draw More, Work Less
PlantUML
Greach - The Groovy Ecosystem
はじめてのUnitTest XCTestに触れて
Git and GitHub
Ad

Similar to GeeCON Prague 2018 - Kotlin DSL in under an hour (20)

PDF
JavaZone 2022 - Building Kotlin DSL.pdf
PPTX
Nice to meet Kotlin
PPTX
Using Kotlin, to Create Kotlin, to Teach Kotlin, in Space
PPTX
PPTX
Create your DSL with Kotlin
PDF
Kotlin DSLs
PDF
Kotlin: Why Do You Care?
PDF
Intro to Kotlin
PDF
Introduction to kotlin
PDF
Sprache als Werkzeug: DSLs mit Kotlin (JAX 2020)
PDF
Coding for Android on steroids with Kotlin
PDF
Why Kotlin is your next language?
PDF
Anko - The Ultimate Ninja of Kotlin Libraries?
PDF
Summer of Tech 2017 - Kotlin/Android bootcamp
PDF
Kotlin boost yourproductivity
PPTX
Kotlin DSL beats the builder pattern
PDF
Android 101 - Building a simple app with Kotlin in 90 minutes
PDF
Building microservices with Kotlin
PDF
Let's fly to the Kotlin Island. Just an introduction to Kotlin
PDF
«Продакшн в Kotlin DSL» Сергей Рыбалкин
JavaZone 2022 - Building Kotlin DSL.pdf
Nice to meet Kotlin
Using Kotlin, to Create Kotlin, to Teach Kotlin, in Space
Create your DSL with Kotlin
Kotlin DSLs
Kotlin: Why Do You Care?
Intro to Kotlin
Introduction to kotlin
Sprache als Werkzeug: DSLs mit Kotlin (JAX 2020)
Coding for Android on steroids with Kotlin
Why Kotlin is your next language?
Anko - The Ultimate Ninja of Kotlin Libraries?
Summer of Tech 2017 - Kotlin/Android bootcamp
Kotlin boost yourproductivity
Kotlin DSL beats the builder pattern
Android 101 - Building a simple app with Kotlin in 90 minutes
Building microservices with Kotlin
Let's fly to the Kotlin Island. Just an introduction to Kotlin
«Продакшн в Kotlin DSL» Сергей Рыбалкин
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

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
Electronic commerce courselecture one. Pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation
Unlocking AI with Model Context Protocol (MCP)
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Encapsulation_ Review paper, used for researhc scholars
Programs and apps: productivity, graphics, security and other tools
Digital-Transformation-Roadmap-for-Companies.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Spectroscopy.pptx food analysis technology
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Machine learning based COVID-19 study performance prediction
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
20250228 LYD VKU AI Blended-Learning.pptx

GeeCON Prague 2018 - Kotlin DSL in under an hour