Slide 1

Slide 1 text

Why we ditched JavaScript for Kotlin/JS? FOSDEM 2023 - JavaScript DevRoom Presented by Louis CAD, Google Developer Expert for Kotlin

Slide 2

Slide 2 text

Why we ditched JavaScript for Kotlin/JS? Please answer

Slide 3

Slide 3 text

Why we ditched JavaScript for Kotlin/JS? Wrong answers only • Because Kotlin is cool 😎 • Because of the hype 🏝 • Because we love to hate JavaScript • Because JavaScript was designed in just 7 days while Kotlin was in 6 years

Slide 4

Slide 4 text

What can we do on the WEB with Kotlin?

Slide 5

Slide 5 text

Demo of play.data2viz.io

Slide 6

Slide 6 text

Demo of play.data2viz.io Forces balls + events Ripped Curtains Wind simulation 3 Forces documentation

Slide 7

Slide 7 text

Demo of charts.kt on play.data2viz.io Stacked Area chart Charts: Bubble Chart Charts.kt color themes

Slide 8

Slide 8 text

What can we do on the WEB with Kotlin? • A lot of complicated things • Manipulate the DOM • Draw on Canvas • Write readable and shareable business logic • Sky is the limit

Slide 9

Slide 9 text

Why we ditched JavaScript for Kotlin/JS! 3 answers. 1. Safety and reliability ✅ 2. Language design ✍ • Concise syntax • Type safety • Cancellable async programming (aka. coroutines) • Many other features… 3. Code sharing 🤗 • Share what you want, native interoperability

Slide 10

Slide 10 text

Why we ditched JavaScript for Kotlin/JS! 3. Code sharing 🤗 


Slide 11

Slide 11 text

Let's start with #3 : Code sharing

Slide 12

Slide 12 text

Let's start with #3 : Code sharing Because sharing is caring…?

Slide 13

Slide 13 text

Code sharing Because sharing is caring…?

Slide 14

Slide 14 text

Code sharing Which platforms does Kotlin run on?

Slide 15

Slide 15 text

Code sharing Which platforms does Kotlin compile to?

Slide 16

Slide 16 text

Kotlin compiles to these platforms • JVM (server, desktop, libraries…) • JavaScript (browser, nodeJS) • Android (& Wear OS) within ART (≃JVM) • Native • iOS, watchOS, macOS, tvOS • Linux • Windows • Android NDK

Slide 17

Slide 17 text

Kotlin runs natively

Slide 18

Slide 18 text

Kotlin runs natively on nearly all devices.

Slide 19

Slide 19 text

Kotlin is not all about Kotlin.

Slide 20

Slide 20 text

Interoperability! Kotlin is not all about Kotlin.

Slide 21

Slide 21 text

interoperability! Kotlin is not all about Kotlin.

Slide 22

Slide 22 text

Kotlin has direct interoperability

Slide 23

Slide 23 text

Kotlin has direct interoperability with the following programming languages • Java • JavaScript • TypeScript (since Kotlin 1.8) • Objective-C • Swift • C

Slide 24

Slide 24 text

Kotlin has direct interoperability with the following programming languages • Java (both ways) • JavaScript (both ways, some quirks) • TypeScript (both ways, some quirks) • Objective-C (both ways, some quirks) • Swift (through Objective-C, & also with dedicated community plugins) • C (both ways, some quirks)

Slide 25

Slide 25 text

What direct interop means • Not reliant on third party plugins updates like in Cordova, Flutter, etc. • Use iOS, Android, JS, Java, & C APIs like they were built in Kotlin • Call new Kotlin code from Java, like it was built in Java* • Call new Kotlin code from Swift, like it was built in Swift* • Call new Kotlin code from JavaScript, like it was built in JavaScript* * limitations might apply

Slide 26

Slide 26 text

That makes Kotlin the most interoperable programming language

Slide 27

Slide 27 text

Why we ditched JavaScript for Kotlin/JS! 3 answers. 1. Safety and reliability ✅ 2. Language design ✍ • Concise syntax • Type safety • Cancellable async programming (aka. coroutines) • Many other features… 3. Code sharing 🤗 • Share what you want, native interoperability

Slide 28

Slide 28 text

Why we ditched JavaScript for Kotlin/JS! 3 answers. 1. Safety and reliability ✅ 
 
 


Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Why we ditched JavaScript for Kotlin/JS! 3 answers. 1. Safety and reliability ✅ 
 
 


Slide 31

Slide 31 text

Safety and reliability

Slide 32

Slide 32 text

Safety and reliability Reason number one.

Slide 33

Slide 33 text

JavaScript be like Your users

Slide 34

Slide 34 text

Your users Applies to other dynamic typed programming languages (Bash, Python, etc…). Missing bolts, wing off? No problem Let me try to f ly it anyway. Let me try to run it anyway. JavaScript be like

Slide 35

Slide 35 text

Kotlin be like

Slide 36

Slide 36 text

Kotlin be like

Slide 37

Slide 37 text

No users. Bolt required 👆 No takeo ff possible. Kotlin be like

Slide 38

Slide 38 text

No users. Wrong bolt type 👉 No takeo ff possible. Kotlin be like

Slide 39

Slide 39 text

No users. Expected a pilot seat but got an economy seat. No takeo ff possible. Kotlin be like

Slide 40

Slide 40 text

You don't have to f ly the plane to realize…

Slide 41

Slide 41 text

Compiling isn't just about browsing the WEB while waiting. You don't have to f ly the plane to realize… …that one wing wasn't attached securely.

Slide 42

Slide 42 text

Compiling can actually save you time! Compiled language means less tests to write

Slide 43

Slide 43 text

What should we do on the WEB with Kotlin? • WEB apps • Webpages/websites with lots of dynamic logic that can't move server-side • Anything that doesn't have to be less than 300kB (i.e. landing page) • Generate html with Kotlin code (even not Kotlin/JS) • Libraries for Kotlin consumers • Complex libraries for Typescript/JavaScript consumers

Slide 44

Slide 44 text

Why we ditched JavaScript for Kotlin/JS! 3 answers. 1. Safety and reliability ✅ 2. Language design ✍ • Concise syntax • Type safety • Cancellable async programming (aka. coroutines) • Many other features… 3. Code sharing 🤗 • Share what you want, native interoperability

Slide 45

Slide 45 text

Why we ditched JavaScript for Kotlin/JS! 2. Language design ✍ • Concise syntax • Type safety • Cancellable async programming (aka. coroutines) • Many other features… 
 
 


Slide 46

Slide 46 text

Basics

Slide 47

Slide 47 text

Variables val attendees = 23

Slide 48

Slide 48 text

Variables val attendees = 23 attendees++

Slide 49

Slide 49 text

Variables val attendees = 23 attendees++ // compilation error Immutable reference

Slide 50

Slide 50 text

Variables var attendees = 23 attendees++ Mutable reference

Slide 51

Slide 51 text

Variables var attendees = 23 attendees++ // ok Mutable reference

Slide 52

Slide 52 text

Functions fun welcomeMessage(name: String): String { 
 return "Welcome back " + name 
 }

Slide 53

Slide 53 text

Functions fun welcomeMessage(name: String): String = "Welcome back " + name

Slide 54

Slide 54 text

Functions fun welcomeMessage(name: String) = "Welcome back " + name

Slide 55

Slide 55 text

Classes class User

Slide 56

Slide 56 text

Classes class User()

Slide 57

Slide 57 text

Classes class User(val firstName: String, val lastName: String, val middleName: String = "")

Slide 58

Slide 58 text

Classes class User( val firstName: String, val lastName: String, val middleName: String = "" )

Slide 59

Slide 59 text

Classes data class User( val firstName: String, val lastName: String, val middleName: String = "" )

Slide 60

Slide 60 text

Classes open class User( val firstName: String, val lastName: String, val middleName: String = "" )

Slide 61

Slide 61 text

Classes open class Vehicle(val name: String) 
 
 class Car(name: String) : Vehicle(name)

Slide 62

Slide 62 text

Classes open class Vehicle(val name: String) 
 
 class Car(name: String) : Vehicle(name), Comparable { 
 override fun compareTo(other: Car): Int { 
 TODO() 
 } 
 }

Slide 63

Slide 63 text

Classes open class Vehicle(val name: String) 
 
 class Car(name: String) : Vehicle(name), Comparable { 
 override fun compareTo(other: Car) = TODO() 
 }

Slide 64

Slide 64 text

Language features

Slide 65

Slide 65 text

Null safety • Nullable? types • NPE safe equals NPE: NullPointerException (similar to TypeError null/unde f ined in JS)

Slide 66

Slide 66 text

Null safety var greeting: String = "Hello World!" 
 greeting = null // compilation error

Slide 67

Slide 67 text

Null safety var greeting: String = "Hello World!" 
 greeting = null

Slide 68

Slide 68 text

Null safety var greeting: String? = "Hello World!" 
 greeting = null

Slide 69

Slide 69 text

Null safety var greeting: String? = "Hello World!" 
 greeting = null // ok

Slide 70

Slide 70 text

Null safety var greeting: String? = "Hello World!" 
 greeting = null // ok 
 greeting.length // compilation error

Slide 71

Slide 71 text

Null safety var greeting: String? = "Hello World!" 
 greeting.length // compilation error

Slide 72

Slide 72 text

Null safety var greeting: String? = "Hello World!" 
 greeting?.length // ok Safe call operator

Slide 73

Slide 73 text

Null safety var greeting: String? = "Hello World!" 
 greeting!!.length Non null assertion

Slide 74

Slide 74 text

Null safety var greeting: String? = "Hello World!" 
 greeting = null greeting!!.length // compiles throws NullPointerException

Slide 75

Slide 75 text

Null safety var greeting: String? = "Hello World!" 
 val chars: Int = greeting?.length // error

Slide 76

Slide 76 text

Null safety var greeting: String? = "Hello World!" 
 val chars: Int = greeting?.length ?: 0 //ok Elvis operator

Slide 77

Slide 77 text

Null safety assert("Bonjour" == "Bonjour")

Slide 78

Slide 78 text

Null safety assert("Bonjour" == "Bonjour") assert “Bonjour".equals("Bonjour");

Slide 79

Slide 79 text

Null safety assert("Bonjour" === "Bonjour") assert “Bonjour” == "Bonjour";

Slide 80

Slide 80 text

Null safety nullableValue == otherNullableValue No NullPointerException!

Slide 81

Slide 81 text

Properties • Similar to f ields, but include getter and setters • Can be delegated (Google for "Kotlin delegated properties")

Slide 82

Slide 82 text

Properties var thing1: Something = someInitialValue 
 val thing2: Whatever = Whatever()

Slide 83

Slide 83 text

Properties var thing1 = someInitialValue 
 val thing2 = Whatever()

Slide 84

Slide 84 text

Properties var stringRepresentation: String 
 get() = this.toString() 
 set(value) { 
 setDataFromString(value) // Parses the string and // assigns values to other properties. 
 }

Slide 85

Slide 85 text

Extension functions • Static methods (no dynamic/surprising resolution) • “instance-method like” syntax • Works even on f inal classes (default in Kotlin)

Slide 86

Slide 86 text

Extension functions if (3.isEven()) println("I don't believe you!") fun Int.isEven() = this % 2 == 0

Slide 87

Slide 87 text

Extension functions if (3.isEven()) println("I don't believe you!") if (9.isMultipleOf(3)) println("For sure!") fun Int.isMultipleOf(other: Int) = this % other == 0 fun Int.isEven() = isMultipleOf(2)

Slide 88

Slide 88 text

Extension functions if (3.isEven()) println("I don't believe you!") if (9 isMultipleOf 3) println("For sure!") infix fun Int.isMultipleOf(other: Int) = this % other == 0 fun Int.isEven() = isMultipleOf(2)

Slide 89

Slide 89 text

Extension functions inline fun Int.hasFlag(flag: Int) = flag and this == flag 
 inline fun Int.withFlag(flag: Int) = this or flag 
 inline fun Int.minusFlag(flag: Int) = this and flag.inv() if (0b1011.hasFlag(0b0001)) println("I think so!") if (0xF003.hasFlag(0x0001)) println("Yes too") if (0x0000.hasFlag(0x0001)) println("Impossible")

Slide 90

Slide 90 text

Higher-order functions Kézako? A function that takes another function as a parameter (or multiple ones)

Slide 91

Slide 91 text

Higher-order functions fun logEntryAndExit( name: String, block: () -> Unit ) { }

Slide 92

Slide 92 text

Higher-order functions fun logEntryAndExit( name: String, block: () -> Unit ) { } logEntryAndExit( )

Slide 93

Slide 93 text

Higher-order functions fun logEntryAndExit( name: String, block: () -> Unit ) { } logEntryAndExit( "Calling fancy code", )

Slide 94

Slide 94 text

Higher-order functions fun logEntryAndExit( name: String, block: () -> Unit ) { } logEntryAndExit( "Calling fancy code", { fancyCode() } )

Slide 95

Slide 95 text

Higher-order functions fun logEntryAndExit( name: String, block: () -> Unit ) { try { } finally { } } logEntryAndExit( "Calling fancy code", { fancyCode() } )

Slide 96

Slide 96 text

Higher-order functions fun logEntryAndExit( name: String, block: () -> Unit ) { try { println("Start: $name") } finally { println("End: $name") } } logEntryAndExit( "Calling fancy code", { fancyCode() } )

Slide 97

Slide 97 text

Higher-order functions fun logEntryAndExit( name: String, block: () -> Unit ) { try { println("Start: $name") block() } finally { println("End: $name") } } logEntryAndExit( "Calling fancy code", { fancyCode() } )

Slide 98

Slide 98 text

Higher-order functions inline fun logEntryAndExit( name: String, block: () -> Unit ) { try { println("Start: $name") block() } finally { println("End: $name") } } logEntryAndExit( "Calling fancy code", { fancyCode() } )

Slide 99

Slide 99 text

Higher-order functions inline fun logEntryAndExit( name: String, block: () -> Unit ) { try { println("Start: $name") block() } finally { println("End: $name") } } logEntryAndExit( name = "Calling fancy code", block = { fancyCode() } )

Slide 100

Slide 100 text

Higher-order functions inline fun logEntryAndExit( name: String, block: () -> Unit ) { try { println("Start: $name") block() } finally { println("End: $name") } } logEntryAndExit("Calling fancy code") { fancyCode() }

Slide 101

Slide 101 text

Lambdas The "anonymous" functions passed to higher-order functions • Can be inlined! Avoids unneeded memory allocation for better perfs.

Slide 102

Slide 102 text

Lambdas aka. anonymous functions inline fun logEntryAndExit( name: String, block: () -> Unit ) { try { println("Start: $name") block() } finally { println("End: $name") } } logEntryAndExit("Calling fancy code") { fancyCode() }

Slide 103

Slide 103 text

Lambdas aka. anonymous functions inline fun logEntryAndExit( name: String, block: () -> Unit ) { try { println("Start: $name") block() } finally { println("End: $name") } } logEntryAndExit( name = "Calling fancy code", block = { fancyCode() } )

Slide 104

Slide 104 text

Lambdas val numbers = intArrayOf(1, 2, 3, 4, 5) 
 numbers.filter { it % 2 == 0 }

Slide 105

Slide 105 text

when expression fun cases(obj: Any) { 
 when (obj) { 
 1 -> println("One") 
 "Hello" -> println("Greeting") 
 is Long -> println("Long") 
 !is String -> println("Not a string") 
 else -> println("Unknown") 
 } 
 }

Slide 106

Slide 106 text

String templates fun greet(name: String) { 
 println("Bonjour $name!") 
 }

Slide 107

Slide 107 text

String templates fun greet(name: String) { 
 println("Bonjour ${name.first().uppercase()}.!") 
 }

Slide 108

Slide 108 text

Built-in singletons • Lazy instantiation • No parameter • Can extend any class

Slide 109

Slide 109 text

Built-in singletons object Unit { 
 override fun toString() = "kotlin.Unit" 
 }

Slide 110

Slide 110 text

Smart casts fun getStringLength(obj: Any): Int? { 
 if (obj is String) 
 return obj.length // no cast to String is needed 
 return null 
 }

Slide 111

Slide 111 text

Kotlin killer feature? Besides best-in-class interop

Slide 112

Slide 112 text

Structured concurrency aka. coroutines, is Kotlin's killer feature

Slide 113

Slide 113 text

What are coroutines? • Saves you from callback hell • A way to write asynchronous code sequentially, without nested callbacks • Similar to async/await from JavaScript and other languages • Also Structured Concurrency

Slide 114

Slide 114 text

What is Structured Concurrency? • A system to prevent concurrency issues • Requires a parent scope to launch a new, concurrent coroutine • Propagates cancellation and crashes at the right place

Slide 115

Slide 115 text

How to get started? Best way to learn and leverage is to try and experiment • Open IntelliJ IDEA (Community Edition is enough) • New Project > Kotlin > Browser application • Learn Kotlin syntax and basics at kotl.in/learn • Learn about Kotlin for JavaScript at kotl.in/js • Learn about Kotlin Multiplatform at kotl.in/multiplatform • Learn about coroutines and structured concurrency at kotl.in/coroutines, with Google search, and by watching Roman Elizarov's talks

Slide 116

Slide 116 text

Questions, comments, ideas… Thank you for listening. LouisCAD on GitHub (social links on my pro fi le) Blog at blog.louiscad.com (you can subscribe) Kotlin