Slide 1

Slide 1 text

Kotlin DSL in under an hour @antonarhipov

Slide 2

Slide 2 text

Why Kotlin?

Slide 3

Slide 3 text

Null-safety Coroutines Multiplatform Syntax Why Kotlin?

Slide 4

Slide 4 text

Null-safety Coroutines Multiplatform Syntax Why Kotlin?

Slide 5

Slide 5 text

Null-safety Coroutines Multiplatform Syntax Why Kotlin?

Slide 6

Slide 6 text

Null-safety Coroutines Multiplatform Syntax Why Kotlin?

Slide 7

Slide 7 text

Null-safety Coroutines Multiplatform Syntax Why Kotlin? DSL

Slide 8

Slide 8 text

Domain-speci fi c, i.e.

Slide 9

Slide 9 text

tailored for the speci fi c task Domain-speci fi c, i.e.

Slide 10

Slide 10 text

External

Slide 11

Slide 11 text

External VS Internal

Slide 12

Slide 12 text

External VS Internal

Slide 13

Slide 13 text

External VS Internal

Slide 14

Slide 14 text

External VS Internal

Slide 15

Slide 15 text

External VS Internal

Slide 16

Slide 16 text

Internal

Slide 17

Slide 17 text

createHTML().html { head { +"Hello!" } body { ul { p { +"This is my awesome text!" } } } } kotlinx.html https://github.com/Kotlin/kotlinx.html

Slide 18

Slide 18 text

Exposed https://github.com/JetBrains/Exposed object Cities : Table() { val id = integer("id").autoIncrement().primaryKey() // Column val name = varchar("name", 50) // Column }

Slide 19

Slide 19 text

Exposed https://github.com/JetBrains/Exposed object Cities : Table() { val id = integer("id").autoIncrement().primaryKey() // Column val name = varchar("name", 50) // Column } 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]}") } }

Slide 20

Slide 20 text

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) {}

Slide 21

Slide 21 text

fun main(args: Array) { 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

Slide 22

Slide 22 text

fun main(args: Array) { 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

Slide 23

Slide 23 text

fun main(args: Array) { 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

Slide 24

Slide 24 text

They all look similar!

Slide 25

Slide 25 text

foo { bar { baz = "Hello!" qux = quux { corge = "Blah" } } }

Slide 26

Slide 26 text

foo { bar { baz = "Hello!" qux = quux { corge = "Blah" } } }

Slide 27

Slide 27 text

foo { bar { baz = "Hello!" qux = quux { corge = "Blah" } } }

Slide 28

Slide 28 text

foo { bar { baz = "Hello!" qux = quux { corge = "Blah" } } }

Slide 29

Slide 29 text

foo { bar(grault = 1) { baz = "Hello!" qux = quux { corge = "Blah" } } }

Slide 30

Slide 30 text

foo { bar(grault = 1) { baz = "Hello!" qux = quux { corge = Blah() } } }

Slide 31

Slide 31 text

foo { bar(grault = 1) { baz = "Hello!" qux = quux { corge = Blah() } } }

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Type-safe builders https://kotlinlang.org/docs/reference/type-safe-builders.html

Slide 35

Slide 35 text

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)

Slide 36

Slide 36 text

//extension property val Client.consoleString: String get() = "${twitter.handle} ${company.name}" //extension method fun Client.toConsoleString(): String { return "${twitter.handle} ${company.name}" }

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

In fi x notation https://kotlinlang.org/docs/reference/functions.html

Slide 40

Slide 40 text

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): LocalDateTime { return this.atTime(n.first, n.second) } infix fun Int.hh(n: Int): Pair { return Pair(this, n) }

Slide 41

Slide 41 text

T.() -> Unit

Slide 42

Slide 42 text

Kotlin { YouTube = "youtube.com/kotlin" Slack = "slack.kotl.in" } me { name = "Anton Arhipov" twitter = "@antonarhipov" slides = speakerdeck.com/antonarhipov } books { courses {