Slide 1

Slide 1 text

Introduction to Kotlin/JS for frontend development @ErikHellman speakerdeck.com/erikhellman/js-for-frontend-development

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

kotlinlang.org

Slide 7

Slide 7 text

Compile target

Slide 8

Slide 8 text

fun sayHello(name: String): String { return "Hello, " + name + "!"; }

Slide 9

Slide 9 text

fun sayHello(name: String): String { return "Hello, $name!"; }

Slide 10

Slide 10 text

fun sayHello(name: String): String = "Hello, $name!";

Slide 11

Slide 11 text

fun sayHello(name: String): String = "Hello, $name!"

Slide 12

Slide 12 text

fun nullAllowed(name: String?) { println("Hello, ${name ?: "You"}!") } Nullability

Slide 13

Slide 13 text

fun fetchData(url: String?): Data? { return url?.let { fetch(it) } } Nullability

Slide 14

Slide 14 text

fun collectionsDemo(strings: List) { strings .filter { it.length > 5 } .map { it.toLowerCase() } .forEach { println(it) } } Collections - operations

Slide 15

Slide 15 text

fun addToList(strings: List, name:String): List { return strings + name } Collections - modifications

Slide 16

Slide 16 text

fun buildKitten(name: String, color:Color = Color.Black, sex: Sex = Sex.Female, race: Race = Race.Siberian): Kitten { return Kitten(name, color, sex, race) } val kitten = buildKitten("Lucy", color = Color.Gray) Functions - default & named arguments

Slide 17

Slide 17 text

fun createThing(id:String, callback: (thing:Thing) -> Unit) { val thing = Thing(id) callback(thing) } val callback = { thing: Thing -> println(thing) } createThing("first", callback) Functions - lambdas

Slide 18

Slide 18 text

fun createThing(id:String, callback: (thing:Thing) -> Unit) { val thing = Thing(id) callback(thing) } createThing("first", { thing -> println(thing) }) Functions - lambdas

Slide 19

Slide 19 text

fun createThing(id:String, callback: (thing:Thing) -> Unit) { val thing = Thing(id) callback(thing) } createThing("first", { println(it) }) Functions - lambdas

Slide 20

Slide 20 text

fun createThing(id:String, callback: (thing:Thing) -> Unit) { val thing = Thing(id) callback(thing) } createThing("first") { println(it) } Functions - lambdas

Slide 21

Slide 21 text

class Pair(val first:String, val second:String) fun createPair(first:String, second:String): Pair { return Pair(first, second) } Functions - extension functions

Slide 22

Slide 22 text

class Pair(val first:String, val second:String) fun String.pairedWith(other:String) = Pair(this, other) fun createPair(first:String, second:String): Pair { return first.pairedWith(second) } Functions - extension functions

Slide 23

Slide 23 text

class Pair(val first:String, val second:String) infix fun String.pairedWith(other:String) = Pair(this, other) fun createPair(first:String, second:String): Pair { return first pairedWith second } Functions - infix functions

Slide 24

Slide 24 text

class Pair(val first:String, val second:String) { operator fun contains(text:String): Boolean { return text == first || text == second } } fun testPair() { val pair = Pair("Hello", "World") if ("World" in pair) { println("Match!") } } Functions - operator overloading

Slide 25

Slide 25 text

class Thing Classes & Interfaces

Slide 26

Slide 26 text

class Thing(val name: String, val id: Int) Classes - constructor

Slide 27

Slide 27 text

class Thing { var name: String? = null var id: Int? = null } Classes - properties

Slide 28

Slide 28 text

class Thing { var name: String? = null var id: Int? = null companion object { fun build(name: String, id: Int): Thing { return Thing().also { it.name = name it.id = id } } } } Classes - companion object

Slide 29

Slide 29 text

Classes - immutable data classes data class Thing(val name: String, val id: Int) val firstThing = Thing("Erik", 1) val secondThing = firstThing.copy(name = "John")

Slide 30

Slide 30 text

Async functions - Kotlin Coroutines fun fetchDemo(url) { window.fetch(url) .then { response -> response.text() } .then { text -> println(text) } .catch { error -> println("Error: $error") } }

Slide 31

Slide 31 text

Async functions - Kotlin Coroutines suspend fun fetchAndShowData(url: String) { try { val response = window.fetch(url).await() val text = response.text().await() println("Test data: $text") } catch (error: Exception) { println("Error: $error") } }

Slide 32

Slide 32 text

Kotlin/JS - compiler input fun main() { println("Hello, World - From Kotlin/JS!") }

Slide 33

Slide 33 text

class Response(val value: T) interface Api { fun load(url: String): Response } interface Ui { fun show(data: T) } DSLs!

Slide 34

Slide 34 text

fun dslDemo(api: Api, ui: Ui) { load ("http:=/api.server.com/v1/things/1") { api.load(it) } then { ui.show(it) } } DSLs!

Slide 35

Slide 35 text

fun load( url:String, loadFunc: (url:String) => Response): Deferred { return async { loadFunc(url).value } } infix fun Deferred.then(lambda: (T) => Unit) { launch { lambda([email protected]()); } } DSLs!

Slide 36

Slide 36 text

fun dslDemo(api: Api, ui: Ui) { load ("http:=/api.server.com/v1/things/1") { api.load(it) } then { ui.show(it) } } DSLs!

Slide 37

Slide 37 text

fun Application.main() { routing { get("/") { call.respondHtml { head { title { +"Ktor: html" } } body { p { +"Hello from Ktor html sample application" } } } } } } Ktor.io - Web DSL in practice!

Slide 38

Slide 38 text

Kotlin/JS compiler $ kotlinc-js Main.kt -output main.js

Slide 39

Slide 39 text

Kotlin/JS - compiler output if (typeof kotlin === 'undefined') { throw new Error("Error loading module 'main'. Its dependency 'kotlin' was not found. Please, check whether 'kotlin' is loaded prior to 'main'."); } var main = function (_, Kotlin) { 'use strict'; var println = Kotlin.kotlin.io.println_s8jyv4$; function main() { println('Hello, World - From Kotlin/JS!'); } _.main = main; main(); Kotlin.defineModule('main', _); return _; }(typeof main === 'undefined' ? {} : main, kotlin);

Slide 40

Slide 40 text

Kotlin/JS - build.gradle.kts plugins { kotlin("js") version "1.3.50" } group = "se.hellsoft" version = "1.0.0" kotlin { target { browser() } sourceSets["main"].dependencies { implementation(kotlin("stdlib-js")) } } repositories { jcenter() }

Slide 41

Slide 41 text

But why?

Slide 42

Slide 42 text

Compile target

Slide 43

Slide 43 text

Thank you for listening! Any questions?

Slide 44

Slide 44 text

Resources ● https://speakerdeck.com/erikhellman/js-for-frontend-development ● https://play.kotlinlang.org/ ● https://kotlinlang.org/ ● https://kotlinconf.com/