SlideShare una empresa de Scribd logo
1 de 41
Descargar para leer sin conexión
Kotlin DSL
in under an hour
@antonarhipov
Yet
another
Yet
DSLanother
Yet
DSLanother
Yet
talk
Domain-specific, i.e.
tailored for the
specific task
Domain-specific, i.e.
External
External VS Internal
External VS Internal
External VS Internal
Internal
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()
}
}
}
foo {
bar(grault = 1) {
baz = "Hello!"
qux = quux {
corge = Blah()
}
}
}
Let’s write
some code!
https://github.com/jonnyzzz/kotlin-dsl-demo
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"
}

Más contenido relacionado

La actualidad más candente

Bodlogiin code
Bodlogiin codeBodlogiin code
Bodlogiin code
orgil
 
Go Profiling - John Graham-Cumming
Go Profiling - John Graham-Cumming Go Profiling - John Graham-Cumming
Go Profiling - John Graham-Cumming
Cloudflare
 
Data structure programs in c++
Data structure programs in c++Data structure programs in c++
Data structure programs in c++
mmirfan
 

La actualidad más candente (20)

Go memory
Go memoryGo memory
Go memory
 
Visualizing ORACLE performance data with R @ #C16LV
Visualizing ORACLE performance data with R @ #C16LVVisualizing ORACLE performance data with R @ #C16LV
Visualizing ORACLE performance data with R @ #C16LV
 
EMC Dojo Golang Meetup Cambridge
EMC Dojo Golang Meetup CambridgeEMC Dojo Golang Meetup Cambridge
EMC Dojo Golang Meetup Cambridge
 
Go Memory
Go MemoryGo Memory
Go Memory
 
Exploring Anko Components, Kotlin, Android
Exploring Anko Components, Kotlin, AndroidExploring Anko Components, Kotlin, Android
Exploring Anko Components, Kotlin, Android
 
Bodlogiin code
Bodlogiin codeBodlogiin code
Bodlogiin code
 
Contributing to an os project
Contributing to an os projectContributing to an os project
Contributing to an os project
 
Go Profiling - John Graham-Cumming
Go Profiling - John Graham-Cumming Go Profiling - John Graham-Cumming
Go Profiling - John Graham-Cumming
 
Source Plugins
Source PluginsSource Plugins
Source Plugins
 
シェル芸でライフハック(特論)
シェル芸でライフハック(特論)シェル芸でライフハック(特論)
シェル芸でライフハック(特論)
 
Data structure programs in c++
Data structure programs in c++Data structure programs in c++
Data structure programs in c++
 
Groovy
GroovyGroovy
Groovy
 
Short story about Node.js debugging
Short story about Node.js debuggingShort story about Node.js debugging
Short story about Node.js debugging
 
Anko - The Ultimate Ninja of Kotlin Libraries?
Anko - The Ultimate Ninja of Kotlin Libraries?Anko - The Ultimate Ninja of Kotlin Libraries?
Anko - The Ultimate Ninja of Kotlin Libraries?
 
2015 555 kharchenko_ppt
2015 555 kharchenko_ppt2015 555 kharchenko_ppt
2015 555 kharchenko_ppt
 
Coroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in PractiseCoroutines for Kotlin Multiplatform in Practise
Coroutines for Kotlin Multiplatform in Practise
 
CRaSH the shell for the Java Virtual Machine
CRaSH the shell for the Java Virtual MachineCRaSH the shell for the Java Virtual Machine
CRaSH the shell for the Java Virtual Machine
 
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
ECMAScript 6: A Better JavaScript for the Ambient Computing EraECMAScript 6: A Better JavaScript for the Ambient Computing Era
ECMAScript 6: A Better JavaScript for the Ambient Computing Era
 
Kotlin Coroutines - the new async
Kotlin Coroutines - the new asyncKotlin Coroutines - the new async
Kotlin Coroutines - the new async
 
Scaling FastAGI Applications with Go
Scaling FastAGI Applications with GoScaling FastAGI Applications with Go
Scaling FastAGI Applications with Go
 

Similar a Devoxx Ukraine 2018 - Kotlin DSL in under an hour

JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfJavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdf
Anton Arhipov
 
Python GTK (Hacking Camp)
Python GTK (Hacking Camp)Python GTK (Hacking Camp)
Python GTK (Hacking Camp)
Yuren Ju
 

Similar a Devoxx Ukraine 2018 - Kotlin DSL in under an hour (20)

JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfJavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdf
 
The Ring programming language version 1.7 book - Part 44 of 196
The Ring programming language version 1.7 book - Part 44 of 196The Ring programming language version 1.7 book - Part 44 of 196
The Ring programming language version 1.7 book - Part 44 of 196
 
Kotlin DSLs
Kotlin DSLsKotlin DSLs
Kotlin DSLs
 
GroovyConsole2
GroovyConsole2GroovyConsole2
GroovyConsole2
 
«Продакшн в Kotlin DSL» Сергей Рыбалкин
«Продакшн в Kotlin DSL» Сергей Рыбалкин«Продакшн в Kotlin DSL» Сергей Рыбалкин
«Продакшн в Kotlin DSL» Сергей Рыбалкин
 
CoreOS in a Nutshell
CoreOS in a NutshellCoreOS in a Nutshell
CoreOS in a Nutshell
 
CoreOS + Kubernetes @ All Things Open 2015
CoreOS + Kubernetes @ All Things Open 2015CoreOS + Kubernetes @ All Things Open 2015
CoreOS + Kubernetes @ All Things Open 2015
 
Programming in Linux Environment
Programming in Linux EnvironmentProgramming in Linux Environment
Programming in Linux Environment
 
Top 28 programming language with hello world for artificial intelligence
Top 28 programming language with  hello world for artificial intelligenceTop 28 programming language with  hello world for artificial intelligence
Top 28 programming language with hello world for artificial intelligence
 
Crystal Rocks
Crystal RocksCrystal Rocks
Crystal Rocks
 
The Ring programming language version 1.8 book - Part 19 of 202
The Ring programming language version 1.8 book - Part 19 of 202The Ring programming language version 1.8 book - Part 19 of 202
The Ring programming language version 1.8 book - Part 19 of 202
 
Sbaw091006
Sbaw091006Sbaw091006
Sbaw091006
 
Kotlin
KotlinKotlin
Kotlin
 
Rust
RustRust
Rust
 
CouchDB Mobile - From Couch to 5K in 1 Hour
CouchDB Mobile - From Couch to 5K in 1 HourCouchDB Mobile - From Couch to 5K in 1 Hour
CouchDB Mobile - From Couch to 5K in 1 Hour
 
Kotlin Austin Droids April 14 2016
Kotlin Austin Droids April 14 2016Kotlin Austin Droids April 14 2016
Kotlin Austin Droids April 14 2016
 
The Ring programming language version 1.10 book - Part 22 of 212
The Ring programming language version 1.10 book - Part 22 of 212The Ring programming language version 1.10 book - Part 22 of 212
The Ring programming language version 1.10 book - Part 22 of 212
 
Python GTK (Hacking Camp)
Python GTK (Hacking Camp)Python GTK (Hacking Camp)
Python GTK (Hacking Camp)
 
2017-06-22 Documentation as code
2017-06-22 Documentation as code2017-06-22 Documentation as code
2017-06-22 Documentation as code
 
Simple Markdown with Ecto and Protocols
Simple Markdown with Ecto and ProtocolsSimple Markdown with Ecto and Protocols
Simple Markdown with Ecto and Protocols
 

Más de Anton Arhipov

Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloadingRiga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Anton Arhipov
 
Devclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервьюDevclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервью
Anton Arhipov
 

Más de Anton Arhipov (20)

Idiomatic kotlin
Idiomatic kotlinIdiomatic kotlin
Idiomatic kotlin
 
TechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервьюTechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервью
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
 
Build pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity and Kotlin DSLBuild pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity and Kotlin DSL
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
 
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainersJavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
 
GeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainersGeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainers
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
 
JavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - TestContainers: integration testing without the hassleJavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - TestContainers: integration testing without the hassle
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
 
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloadingJavaZone 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
JUG.ua 20170225 - Java bytecode instrumentationJUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentation
 
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloadingRiga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
 
GeeCON 2017 - TestContainers. Integration testing without the hassle
GeeCON 2017 - TestContainers. Integration testing without the hassleGeeCON 2017 - TestContainers. Integration testing without the hassle
GeeCON 2017 - TestContainers. Integration testing without the hassle
 
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingJEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with Javassist
 
Devclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервьюDevclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервью
 
Joker 2016 - Bytecode 101
Joker 2016 - Bytecode 101Joker 2016 - Bytecode 101
Joker 2016 - Bytecode 101
 
JPoint 2016 - Etudes of DIY Java profiler
JPoint 2016 - Etudes of DIY Java profilerJPoint 2016 - Etudes of DIY Java profiler
JPoint 2016 - Etudes of DIY Java profiler
 

Último

Último (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Devoxx Ukraine 2018 - Kotlin DSL in under an hour