one language, all tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin...

44

Upload: others

Post on 30-May-2020

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests
Page 2: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

@intelliyoleDmitry Jemerov

One Language, All Tiers:

Developing Multiplatform Projects in Kotlin

Page 3: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

What is Kotlin

• Modern programming language • Statically typed • Concise, safe, pragmatic • Interoperable • Targeting the JVM Multiplatform

Page 4: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Server

Android

BrowserKotlin/JVM

Kotlin/JVM

Kotlin/JS

Kotlin/Anywhere

Page 5: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Kotlin/JS

Page 6: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

*.d.ts ts2kt

kotlinc*.kt *.js

Compiling a Kotlin/JS project

webpack bundle.js

*.js

Page 7: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

External declarationsexternal abstract class Window { val location: Location fun alert(message: String) // …}

external val window: Window

Page 8: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

dynamic type

val response: dynamic = loadJson("example.com/api")val text = response.messages[0].text

Page 9: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

React support

• Official bindings for react • https://github.com/jetbrains/kotlin-wrappers

• CLI tool to create React application • npm install -g create-react-kotlin-app• create-react-kotlin-app my-app

Page 10: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

kotlin-frontend-plugin

• Download JS dependencies from npm • Create bundle using webpack • Run tests using Karma • Hot reload of changes

Page 11: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Multiplatform projects

Page 12: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Platformmodule

Platformmodule

Commonmodule

Page 13: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Common *.kt

Kotlin/JVM *.kt, *.java

Kotlin/JS *.kt

*.class

*.js

Page 14: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

package kotlin.date

expect class Date { … fun getFullYear(): Int …} Common

JS

package kotlin.date

actual external class Date { actual fun getFullYear(): Int}

package kotlin.date

actual class Date { private val calendar: Calendar … actual fun getFullYear() = calendar[YEAR] …} JVM

Page 15: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

fun isSameDay(from: Date, to: Date) = from.getFullYear() == to.getFullYear() && ...

package kotlin.date

expect class Date { … fun getFullYear(): Int …}

Common

Page 16: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Why not interfaces?

Common

expect class Date { constructor(value: Number)}

expect fun parseDate(dateString: String): Date

expect fun Date.toReadableDateString(): String

Page 17: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Reusing existing implementations

package kotlin.test

actual typealias Test = org.junit.TestJVM

package kotlin.test

expect annotation class Test()Common

Page 18: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Building a common module

apply plugin: 'kotlin-platform-common'

dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-common:$version" }

Common

Page 19: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

apply plugin: 'kotlin-platform-jvm'

dependencies { compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$version" expectedBy project(":konf-common") }

JVM

Building a platform module

Page 20: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Common *.kt

Kotlin/JVM *.kt, *.java

Kotlin/JS *.kt

foo.jar { *.class }

foo-js.jar { *.js, *.kotlin_module }

foo-common.jar { *.kotlin_metadata }

Common Libraries

Page 21: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Multiplatform libraries

Page 22: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Standard library

• Strings • Collections • Higher-order utility functions (with, apply etc.) • Exceptions

Page 23: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

kotlin.testimport kotlin.test.Testimport kotlin.test.assertEquals

class DateTest { @Test fun testParse() { val date = parseDate("2017-11-02") assertEquals(2017, date.getFullYear()) }}

Page 24: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

kotlinx.html

div { a("http://kotlinlang.org") { target = ATarget.blank +"Main site" }}

Page 25: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

kotlinx.html

div { aNewWindow("http://kotlinlang.org") { +"Main site" }}

Page 26: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

kotlinx.serialization

• @Serializable• Compiler plugin to generate serialization calls (no

reflection) • JSON and ProtoBuf support

Page 27: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Future plans

• I/O • Networking (TCP, HTTP etc.) • Dates • …more?

Page 28: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

DEMO

Page 29: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Kotlin/Native

Page 30: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

ServerAndroid

iOS Browser

Kotlin/JVMKotlin/JVM

Kotlin/Native Kotlin/JS

Page 31: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

iOS appLLVM-based toolchain

NativeBinaryLLVM

Page 32: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

iOS app

App

C

lib lib

C

lib

Obj-C

CoreLibs

Page 33: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

cinterop

• Clang-based tool to generate Kotlin metadata from C header files

• Supports all C types: strings, pointers, structs, callbacks

• Supports Objective-C types

Page 34: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

iOS libraries

Page 35: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

iOS libraries

fun showGameCenter() { val gkViewController = GKGameCenterViewController().apply { gameCenterDelegate = this@ViewController viewState = GKGameCenterViewControllerStateLeaderboards leaderboardTimeScope = GKLeaderboardTimeScopeToday leaderboardCategory = "main" }

this.presentViewController(gkViewController, animated = true, completion = null)}

Page 36: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Kotlin/Native Memory management

• Reference counting + cycle collector • No shared memory between threads • Still under design; final version likely to be different

Page 37: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Kotlin/Native Target Platforms

• Windows, Linux, macOS • iOS, Android • WebAssembly • Embedded?

Page 38: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Tooling

• Plugin for CLion • All features of regular Kotlin plugin • Debugger • Test runner

Page 39: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Tooling

Page 40: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Tooling

Page 41: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

Summary

• Kotlin's goal is to allow you to write all parts of your app in the same language

• Full interoperability on every supported platform • Shared business logic, platform-specific UI • JVM/JS code reuse available today, native coming

soon

Page 42: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

To learn more

• http://kotlinlang.org/ • http://blog.jetbrains.com/kotlin • http://slack.kotlinlang.org/ • "Kotlin in Action" book

Page 43: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests

@intelliyoleDmitry Jemerov

Thank you!

Page 44: One Language, All Tiers...2017/11/16  · • create-react-kotlin-app my-app kotlin-frontend-plugin • Download JS dependencies from npm • Create bundle using webpack • Run tests