Slide 1

Slide 1 text

Kotlin DSL in under an hour @antonarhipov

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

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?

Slide 8

Slide 8 text

Null-safety Coroutines Multiplatform Syntax Why Kotlin? DSL

Slide 9

Slide 9 text

“Domain-speci fi c”, i.e.

Slide 10

Slide 10 text

tailored for a speci fi c task “Domain-speci fi c”, i.e.

Slide 11

Slide 11 text

External

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

External VS Internal

Slide 17

Slide 17 text

Internal

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

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

Slide 21

Slide 21 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 22

Slide 22 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 23

Slide 23 text

fun main() { embeddedServer(Netty, port = 8080, host = “0.0.0.0", module = Application :: module).start(wait = true) } fun Application.main() { install(DefaultHeaders) install(CallLogging) routing { get("/") { call.respondHtml { head { title { +"Ktor App" } } body { p { +"Hello from Ktor!" } Ktor + kotlinx.html

Slide 24

Slide 24 text

fun main() { embeddedServer(Netty, port = 8080, host = “0.0.0.0", module = Application :: module).start(wait = true) } fun Application.main() { install(DefaultHeaders) install(CallLogging) routing { get("/") { call.respondHtml { head { title { +"Ktor App" } } body { p { +"Hello from Ktor!" } Ktor + kotlinx.html

Slide 25

Slide 25 text

fun main() { embeddedServer(Netty, port = 8080, host = “0.0.0.0", module = Application :: module).start(wait = true) } fun Application.main() { install(DefaultHeaders) install(CallLogging) routing { get("/") { call.respondHtml { head { title { +"Ktor App" } } body { p { +"Hello from Ktor!" } Ktor + kotlinx.html

Slide 26

Slide 26 text

+

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

They all look similar!

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 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 43

Slide 43 text

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

Slide 44

Slide 44 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 functions

Slide 45

Slide 45 text

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

Slide 46

Slide 46 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 47

Slide 47 text

Context Receivers KEEP-259 Experimental

Slide 48

Slide 48 text

context(DateContext) @DateDsl infix fun Int.March(n: Int): LD = LD.of(n, Month.MARCH, this) class DateContext Context object Context requirement

Slide 49

Slide 49 text

context(DateContext) @DateDsl infix fun Int.March(n: Int): LD = LD.of(n, Month.MARCH, this) class DateContext 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

Slide 50

Slide 50 text

context(DateContext) @DateDsl infix fun Int.March(n: Int): LD = LD.of(n, Month.MARCH, this) class DateContext val client = client { // .. . 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

Slide 51

Slide 51 text

T.() -> Unit

Slide 52

Slide 52 text

kotlin { youtube = "youtube.com/kotlin" slack = "slack.kotl.in" } me { name = "Anton Arhipov" twitter = "@antonarhipov" slides = speakerdeck.com/antonarhipov code = github.com/antonarhipov }