1 of 63

Kotlin DSLs �in 42 minutes

_

Eugene Petrenko

eugene.petrenko@jetbrains.com

@jonnyzzz

Munich, 02 November, 2017

JetBrains, @jonnyzzz, Devoxx

2 of 63

@jonnyzzz

JetBrains, Kotlin user, phd

Blogger https://jonnyzzz.com

Kotlin, Java, Go, C/C++, Qt, Toolbox App

Server-side, Desktop, DSLs, fun!

JetBrains, @jonnyzzz, Devoxx

3 of 63

DSL

A domain-specific language (DSL) is a computer language specialized to a particular application domain. This is in contrast to a general-purpose language (GPL), which is broadly applicable across domains...

(from Wikipedia)

JetBrains, @jonnyzzz, Devoxx

4 of 63

Specific DSLs

SQL / Regexp

State machines

Custom Stack�JetBrains MPS can help!

JetBrains, @jonnyzzz, Devoxx

5 of 63

Embedded DSLs

Uses existing language

Java-style builders

Fluent APIs

Your current stack

JetBrains, @jonnyzzz, Devoxx

6 of 63

JetBrains, @jonnyzzz, Devoxx

7 of 63

DSL

Easy to read, � express, � understand

�Configuration / Templates / Logic / UI / ...

JetBrains, @jonnyzzz, Devoxx

8 of 63

JetBrains, @jonnyzzz, Devoxx

9 of 63

JetBrains, @jonnyzzz, Devoxx

10 of 63

JetBrains, @jonnyzzz, Devoxx

11 of 63

JetBrains, @jonnyzzz, Devoxx

12 of 63

Why Kotlin?

Statically typed + Apache 2.0

Kotlin Syntax 💖 DSLs

IDE + Code Completion

JVM / Android / JS / Native

JetBrains, @jonnyzzz, Devoxx

13 of 63

JetBrains, @jonnyzzz, Devoxx

14 of 63

Coding Time!

JetBrains, @jonnyzzz, Devoxx

15 of 63

The Loop

16 of 63

JetBrains, @jonnyzzz, Devoxx

17 of 63

JetBrains, @jonnyzzz, Devoxx

18 of 63

kotlinx.html library

JetBrains, @jonnyzzz, Devoxx

19 of 63

GuardedBy(“lock”)

JetBrains, @jonnyzzz, Devoxx

20 of 63

JetBrains, @jonnyzzz, Devoxx

21 of 63

JetBrains, @jonnyzzz, Devoxx

22 of 63

JetBrains, @jonnyzzz, Devoxx

23 of 63

What I forgot (at least)

Overloaded Operators� a `foo infix bar` b + c[t]

Delegated properties and impl� val x by lazy { … }� object A : B by c { ... }

24 of 63

The Loop

25 of 63

DSL. The Loop

Imagine a DSL

Implement it

Repeat

26 of 63

JetBrains, @jonnyzzz, Devoxx

27 of 63

JetBrains, @jonnyzzz, Devoxx

28 of 63

Existing DSLs

JetBrains, @jonnyzzz, Devoxx

29 of 63

kotlin.xml.bind

JetBrains, @jonnyzzz, Devoxx

30 of 63

Gradle Script Kotlin

A statically typed DSL for Gradle (instead of Groovy)

github.com/gradle/kotlin-dsl

JetBrains, @jonnyzzz, Devoxx

31 of 63

TeamCity Build DSL

JetBrains, @jonnyzzz, Devoxx

32 of 63

Spring Framework 5

Handler Mapping builders and much more!

spring.io/blog/...kotlin-apis...

JetBrains, @jonnyzzz, Devoxx

33 of 63

Spek

JetBrains, @jonnyzzz, Devoxx

34 of 63

And more. Create yours!

JetBrains, @jonnyzzz, Devoxx

35 of 63

Q & A

JetBrains, @jonnyzzz, Devoxx

36 of 63

Q & A

_

Ask me / Follow me @jonnyzzz

JetBrains, @jonnyzzz, Devoxx

37 of 63

JetBrains, @jonnyzzz, Devoxx

38 of 63

NO TRESPASSING

beyond this slide

JetBrains, @jonnyzzz, Devoxx

39 of 63

Coding Time 2!

JetBrains, @jonnyzzz, Devoxx

40 of 63

Building DSLs help

JetBrains, @jonnyzzz, Devoxx

41 of 63

The Loop

42 of 63

Anko

Anko is a Kotlin library which makes Android application development faster and easier.

https://github.com/Kotlin/anko

JetBrains, @jonnyzzz, Devoxx

43 of 63

GuardedBy(“lock”)

JetBrains, @jonnyzzz, Devoxx

44 of 63

Coding Time 2!

JetBrains, @jonnyzzz, Devoxx

45 of 63

Included in Kotlin

Standard library

kotlinx.coroutines

JetBrains, @jonnyzzz, Devoxx

46 of 63

JetBrains, @jonnyzzz, Devoxx

47 of 63

Useful Links

JetBrains, @jonnyzzz, Devoxx

48 of 63

Exposed aka Kotlin SQL Library

Exposed is a prototype for a lightweight SQL library... �typesafe SQL wrapping DSL and lightweight data access objects

https://github.com/JetBrains/Exposed

JetBrains, @jonnyzzz, Devoxx

49 of 63

kotlin.xml.dsl

JetBrains, @jonnyzzz, Devoxx

50 of 63

Story: Config DSLs

A XML configuration files editor via Kotlin DSL

jonnyzzz/TeamCity2DSL

jonnyzzz/Log4j2DSL

JetBrains, @jonnyzzz, Devoxx

51 of 63

Java Server Side

Internal project. Java 1.8, SpringFramework, GRPC, Gradle, HTTP, Jetty

Use from January 2016

New code 99% Kotlin code: � 698 Java vs 404 Kotlin files

JetBrains, @jonnyzzz, Devoxx

52 of 63

Story: TeamCity.Node

Support for NodeJS related build runners for TeamCity

Released on 14 January 2013

github.com/jonnyzzz/TeamCity.Node

jonnyzzz.com/blog/2013/01/14/kotlin-nodejs-and-teamcity/

JetBrains, @jonnyzzz, Devoxx

53 of 63

Building Kotlin

CMD, Ant

Gradle, Maven

Kobalt

Webpack

IntelliJ IDEA, Android Studio

...

JetBrains, @jonnyzzz, Devoxx

54 of 63

JetBrains, @jonnyzzz, Devoxx

55 of 63

Back in 2010: Why Kotlin?

A better language for JetBrains

plus experience in tooling & languages support

C#, Scala, Groovy, JavaScript, C/C++, …

Static, Concise, Effective, Toolable, Interoperable, Pragmatic

JetBrains, @jonnyzzz, Devoxx

56 of 63

Multi Platform Kotlin

JVM

Android (Dalvik)

JavaScript

Native (LLVM), preview

JetBrains, @jonnyzzz, Devoxx

57 of 63

Kotlin in 42 minutes

JetBrains, @jonnyzzz, Devoxx

58 of 63

Show me the code!

JetBrains, @jonnyzzz, Devoxx

59 of 63

100% Java Interop

We have 17+ years codebase in Java

No way to re-write it

.kt and .java files are OK in one module/project

JetBrains, @jonnyzzz, Devoxx

60 of 63

100% Android Friendly

Java bytecode version is 1.6

All language features �(and lambdas) are available

Official on Android

JetBrains, @jonnyzzz, Devoxx

61 of 63

Kotlin for JavaScript

Static typed web programming

Reuse code between platforms

Webpack, React, Gradle support

JetBrains, @jonnyzzz, Devoxx

62 of 63

Kotlin / Native

Preview, Work in Progress

No VM & fast

LLVM� macOS, iOS� Linux, r. Pi

Soon:Windows

JetBrains, @jonnyzzz, Devoxx

63 of 63

JetBrains, @jonnyzzz, Devoxx