Kotlin DSLs �in 42 minutes
_
JetBrains, @jonnyzzz, Devoxx
@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
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
Specific DSLs
SQL / Regexp
State machines
Custom Stack�JetBrains MPS can help!
JetBrains, @jonnyzzz, Devoxx
Embedded DSLs
Uses existing language
Java-style builders
Fluent APIs
Your current stack
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
DSL
Easy to read, � express, � understand
�Configuration / Templates / Logic / UI / ...
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
Why Kotlin?
Statically typed + Apache 2.0
Kotlin Syntax 💖 DSLs
IDE + Code Completion
JVM / Android / JS / Native
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
Coding Time!
JetBrains, @jonnyzzz, Devoxx
The Loop
Extension Functions� https://kotlinlang.org/docs/reference/extensions.html
Lambdas� https://kotlinlang.org/docs/reference/lambdas.html
Type-Safe builders� https://kotlinlang.org/docs/reference/type-safe-builders.html
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
kotlinx.html library
JetBrains, @jonnyzzz, Devoxx
GuardedBy(“lock”)
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
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 { ... }�
The Loop
Type-Safe builders� https://kotlinlang.org/docs/reference/type-safe-builders.html
Delegated properties� https://kotlinlang.org/docs/reference/delegated-properties.html
Operator overloading� https://kotlinlang.org/docs/reference/operator-overloading.html
DSL. The Loop
Imagine a DSL
Implement it
Repeat
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
Existing DSLs
JetBrains, @jonnyzzz, Devoxx
kotlin.xml.bind
JetBrains, @jonnyzzz, Devoxx
Gradle Script Kotlin
JetBrains, @jonnyzzz, Devoxx
TeamCity Build DSL
JetBrains, @jonnyzzz, Devoxx
Spring Framework 5
JetBrains, @jonnyzzz, Devoxx
Spek
JetBrains, @jonnyzzz, Devoxx
And more. Create yours!
JetBrains, @jonnyzzz, Devoxx
Q & A
JetBrains, @jonnyzzz, Devoxx
Q & A
_
Ask me / Follow me @jonnyzzz
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
NO TRESPASSING
beyond this slide
JetBrains, @jonnyzzz, Devoxx
Coding Time 2!
JetBrains, @jonnyzzz, Devoxx
Building DSLs help
JetBrains, @jonnyzzz, Devoxx
The Loop
Type-Safe builders� https://kotlinlang.org/docs/reference/type-safe-builders.html
Delegated properties� https://kotlinlang.org/docs/reference/delegated-properties.html
Operator overloading� https://kotlinlang.org/docs/reference/operator-overloading.html
Anko
Anko is a Kotlin library which makes Android application development faster and easier.
�https://github.com/Kotlin/anko
JetBrains, @jonnyzzz, Devoxx
GuardedBy(“lock”)
JetBrains, @jonnyzzz, Devoxx
Coding Time 2!
JetBrains, @jonnyzzz, Devoxx
Included in Kotlin
Standard library
kotlinx.coroutines
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
Useful Links
JetBrains, @jonnyzzz, Devoxx
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
kotlin.xml.dsl
JetBrains, @jonnyzzz, Devoxx
Story: Config DSLs
JetBrains, @jonnyzzz, Devoxx
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
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
Building Kotlin
CMD, Ant
Gradle, Maven
Kobalt
Webpack
IntelliJ IDEA, Android Studio
...
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx
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
Multi Platform Kotlin
JVM
Android (Dalvik)
JavaScript
Native (LLVM), preview
JetBrains, @jonnyzzz, Devoxx
Kotlin in 42 minutes
JetBrains, @jonnyzzz, Devoxx
Show me the code!
JetBrains, @jonnyzzz, Devoxx
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
100% Android Friendly
Java bytecode version is 1.6
All language features �(and lambdas) are available
Official on Android
JetBrains, @jonnyzzz, Devoxx
Kotlin for JavaScript
Static typed web programming
Reuse code between platforms
Webpack, React, Gradle support
JetBrains, @jonnyzzz, Devoxx
Kotlin / Native
Preview, Work in Progress
No VM & fast
LLVM� macOS, iOS� Linux, r. Pi
Soon:Windows
JetBrains, @jonnyzzz, Devoxx
JetBrains, @jonnyzzz, Devoxx