Christopher Ng
Kotlinian @
Editor @ blacklenspub.com
Kotlin Wonderland
We love JVM
Single page app?
IoT?
TensorFlow?
iOS?
Let’s say goodbye
to JVM
Kotlin JavaScript
Kotlin.js
Main.jsMain.kt
Kotlin/JS
fun main(args: Array<String>) {
println("Hello JavaScript!")
}
var ConsoleOutput = function (Kotlin) {
'use strict';
var _ = Kotlin.defineRootPackage(null, {
main_kand9s$: function (args) {
Kotlin.println('Hello JavaScript!');
}
});
Kotlin.defineModule('ConsoleOutput', _);
_.main_kand9s$([]);
return _;
}(kotlin);
transpile
group 'com.babedev'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.4'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin2js'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
}
compileKotlin2Js {
kotlinOptions.outputFile = "${projectDir}/web/output.js"
kotlinOptions.moduleKind = "commonjs"
}
build.doLast {
configurations.compile.each { File file ->
copy {
includeEmptyDirs = false
from zipTree(file.absolutePath)
into "${projectDir}/web"
include { fileTreeElement ->
def path = fileTreeElement.path
path.endsWith(".js") &&
(path.startsWith("META-INF/resources/") || !path.startsWith("META-INF/"))
}
}
}
}
JS Stdlib
- org.w3c.dom
- org.w3c.fetch
- org.w3c.xhr
- org.w3c.files
- org.w3c.notifications
- kotlin.js
org.w3c.dom
// Kotlin
val img = document.createElement("img")
img.setAttribute("src",
"https://kotlinlang.org/assets/images/open-graph/kotlin_250x250.png")
val div = document.getElementById("app")
div?.appendChild(img)
// Javascript
var img = document.createElement("img");
img.setAttribute("src",
"https://kotlinlang.org/assets/images/open-graph/kotlin_250x250.png");
var div = document.getElementById("app");
div != null ? div.appendChild(img) : null;
org.w3c.fetch
window.fetch("https://httpbin.org/ip")
.then({ response ->
response.json().then({ body ->
console.log(body)
})
})
.catch({ error ->
console.error(error.message)
})
Dynamic Type
val database = js("firebase.database()")!!
database.ref("/messages")
.on("value") { snapshot: dynamic ->
snapshot.forEach(fun (child: dynamic) {
console.log(child.`val()`)
})
}
External
// Javascript
function greet(msg) {
console.log(msg)
}
// Kotlin
external fun greet(msg: Any?): Unit
greet("Hello Kotlin")
Let’s try
https://github.com/ThaiKotlinDev/kotlinjs-wonderland
Should try
https://github.com/Kotlin/kotlin-fullstack-sample
Kotlin Native
Non-JVM Targets
- Embedded systems / IoT
- iOS
- Windows
- Web Assembly
Main.kt
Kotlin/Native
LLVM
Executable app
http://hadihariri.com/2017/05/28/raspberry-pi-starter-kit-and-kotlin-native/
import pigpio.*
fun main(args: Array<String>) {
const val port = 21
if (gpioInitialise() < 0) {
println("Error initialising")
} else {
if (gpioSetMode(port, PI_OUTPUT) < 0) {
println("Could not set mode for GPIO$port")
} else {
if (gpioWrite(port, 1) < 0) {
println("Could not write to GPIO$port")
}
}
}
}
http://justmaku.org/2017-06-07-kotlin-on-ios
Kotlin Native
C++ Wrapper
Objective-C Bridging Header
Swift
Try it
https://github.com/JetBrains/kotlin-native/tree/master/samples
Swift to Kotlin
https://github.com/angelolloqui/SwiftKotlin
Christopher Ng
Kotlinian @
Editor @ blacklenspub.com
"Thank you".repeat(infinity)

Kotlin wonderland