Kotlin?
● Open Source language, created back in 2010 by
JetBrains
● Statically typed, safe, and pragmatic
● It's design is inspired by languages like Java,
Scala, C#, and Groovy
● Object Oriented with some Functional features
3
Slide 4
Slide 4 text
Lines of Code written in Kotlin on Github
Slide 5
Slide 5 text
No content
Slide 6
Slide 6 text
ADOPT
Slide 7
Slide 7 text
https://pusher.com/state-of-kotlin
Slide 8
Slide 8 text
Cool features
8
DATA
CLASSES
TYPE
ALIAS
LAMBDA
DSL
NULL
SAFETY
PROPERTIES
EXTENSION
FUNCTIONS
Slide 9
Slide 9 text
Platforms
9
JVM JS NATIVE
ANDROID
Slide 10
Slide 10 text
Why to use Kotlin for Android development?
● Compatibility - Java 6
● Performance
● Interoperability
● Footprint: standard library & runtime costs only ~100Kb
● Compilation Time is not a problem anymore
● Learning curve
10
Slide 11
Slide 11 text
Why to use Kotlin for JVM development?
● Expressivity
● Scalability & Performance - Kotlin Coroutines
● Interoperability with existing codebase
● Gradual migration
● Tooling
● Learning curve
11
Slide 12
Slide 12 text
https://bit.ly/2jV4utA
Slide 13
Slide 13 text
JavaFX Framework for Kotlin
https://github.com/edvin/tornadofx
Slide 14
Slide 14 text
Ktor for building servers and clients
https://ktor.io
Slide 15
Slide 15 text
Why to use Kotlin for JavaScript development
● Supports ECMAScript 5.1 on Kotlin 1.2 - ECMAScript 2015 support soon
● Optimized JavaScript - DCE (Dead Code Elimination)
● Legible and debuggable JavaScript
● Compatibility with existing JavaScript code
● Same features as the JVM Standard Library - including Coroutines
15
Slide 16
Slide 16 text
Kotlin for JavaScript - what about types?
● It's possible to interact with any JavaScript code
● Statically-typed APIs / TypeScript -> https://github.com/kotlin/ts2kt
● Dynamic types for any other scenarios
16
Slide 17
Slide 17 text
https://github.com/JetBrains/kotlin-wrappers
Slide 18
Slide 18 text
KotlinJS - jQuery
fun main(args: Array) {
jq("#message").html("Hello from Kotlin")
}
18
Slide 19
Slide 19 text
KotlinJS - React
class HelloComponent: RComponent() {
override fun RBuilder.render() {
div(classes = "content") {
h1 {
+"Hello!"
}
}
}
}
fun RBuilder.hello() = child(HelloComponent::class) { }
19
Slide 20
Slide 20 text
KotlinJS - NodeJS
fun main(args: Array) {
val express = require("express")
val app = express()
app.get("/", { req, res ->
res.type("text/plain")
res.send("i am a beautiful butterfly")
})
app.listen(3000, {
println("Listening on port 3000")
})
} 20
Slide 21
Slide 21 text
Why to use Kotlin for native development?
● Native code, without VM
● LLVM based backend
● Interoperability with any native code and libraries - you can generate
the binding interface using .h files
● MacOS / iOS
● Latest version is 0.8.2 - in active development
21
Slide 22
Slide 22 text
Kotlin Native supported targets
● Windows (x86_64)
● Linux (x86_64, arm32, MIPS, and MIPS little endian)
● MacOS (x86_64)
● iOS (arm32, arm64, x64)
● Android (arm32 e arm64)
● STM32
● WebAssembly (wasm32)
22
Kotlin Native - C interop
fun fetch() {
val res = curl_easy_perform(curl)
if (res != CURLE_OK)
println("curl_easy_perform() failed")
}
fun CPointer.toKString(length: Int): String {
val bytes = this.readBytes(length)
return bytes.stringFromUtf8()
} 25
Multiplatform Kotlin
● Support introduced in 1.2
● JVM & JavaScript (Native is "kind of" working)
● common, platform and regular modules
29
Slide 30
Slide 30 text
Multiplatform Modules
common: Contains common code that can runs on any platform. Also can
hold common interfaces and class signatures that must have a specific
implementation in each platform
platform: Contains the specific implementation of some interface defined by
a common module
regular: A regular module that contains only code to a specific platform
30
Multiplatform Module - Common code
package com.example.foo
expect class Foo(bar: String) {
fun frob()
}
fun main(args: Array) {
Foo("Hello").frob()
}
39
Slide 40
Slide 40 text
Multiplatform Module - specific implementation
package com.example.foo
actual class Foo actual constructor(val bar: String) {
actual fun frob() {
println("Frobbing the $bar")
}
}
40
Slide 41
Slide 41 text
Multiplatform Dependency Injection
41
Slide 42
Slide 42 text
Multiplatform Mock
42
Slide 43
Slide 43 text
KOTLIN GRADLE DSL
Bonus - why not to use Kotlin everywhere?
Slide 44
Slide 44 text
Why to use Kotlin DSL on Gradle?
● Speed up analysis time and compilation time of
build scripts, compared to Groovy
● IDE friendly, better autocomplete
● Unify the language used in the project
● 1.0 RC6 included in Gradle 4.10.1, final to be
included in Gradle 5.0 (next version)
44
Links
OFFICIAL BLOG
https://blog.jetbrains.com/kotlin
KOTLIN KOANS
https://kotlinlang.org/docs/tutorials/koans.html
KOTLIN IN ACTION
https://www.manning.com/books/kotlin-in-action
52
Slide 53
Slide 53 text
THANK YOU!
Questions and feedbacks?
Rafael Toledo
rdtoledo@thoughtworks.com
speakerdeck.com/rafaeltoledo
53