you can do better with kotlin...android studio is based on intellij idea just another library for...

108
Svetlana Isakova You can do better with Kotlin

Upload: others

Post on 12-Jul-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Svetlana Isakova

You can do better with Kotlin

Page 2: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

- modern - pragmatic - Android-friendly

Kotlin Programming Language

Page 3: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Official on Android

Page 4: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Not only Android

Page 5: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Pragmatic

- tooling - Java interop

Page 6: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

From

Page 7: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

has good tooling

Page 8: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

- completion - navigation - refactorings - inspections

Page 9: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

can be easily mixed with Java code

Page 10: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

*.java

*.class

*.dex

compiled to Java bytecode

*.kt

Page 11: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Kotlin code

Java code

You can have Java & Kotlin code in one project

Page 12: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

You can gradually add Kotlin to your existing app

Page 13: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Android-friendly

Page 14: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Android Studio is based on IntelliJ IDEA

Page 15: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

just another library for your app

rxjava-2.1.2

kotlin-stdlib-1.1.4 6315

10212

Page 16: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

No Kotlin SDK…just JDK + extensions

small runtime jar easy Java interop

Page 17: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Modern

- concise - safe - expressive

Page 18: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

concise

Page 19: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

public class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; }}

Page 20: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

- equals - hashCode - toString

data

class Person(val name: String, val age: Int)

Page 21: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

public class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; }}

class Person( val name: String, val age: Int )

person.nameperson.getName()

Page 22: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

class Person( val name: String, val age: Int )

person.getName()

Page 23: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

public class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; }}

person.name

Page 24: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

public void updateWeather(int degrees) { String description; Colour colour; if (degrees < 5) { description = "cold"; colour = BLUE; } else if (degrees < 23) { description = "mild"; colour = ORANGE; } else { description = "hot"; colour = RED; } // ...}

enum Colour { BLUE, ORANGE, RED, /*...*/; }

Page 25: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

fun updateWeather(degrees: Int) { val description: String val colour: Colour if (degrees < 5) { description = "cold" colour = BLUE } else if (degrees < 23) { description = "mild" colour = ORANGE } else { description = "hot" colour = RED } // ...}

Page 26: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

fun updateWeather(degrees: Int) { val (description: String, colour: Colour) = if (degrees < 5) { Pair("cold", BLUE) } else if (degrees < 23) { Pair("mild", ORANGE) } else { Pair("hot", RED) } // ...}

Page 27: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

fun updateWeather(degrees: Int) { val (description, colour) = if (degrees < 5) { Pair("cold", BLUE) } else if (degrees < 23) { Pair("mild", ORANGE) } else { Pair("hot", RED) } // ...}

Page 28: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

fun updateWeather(degrees: Int) { val (description, colour) = when { degrees < 5 -> Pair("cold", BLUE) degrees < 23 -> Pair("mild", ORANGE) else -> Pair("hot", RED) } // ...}

Page 29: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

fun updateWeather(degrees: Int) { val (description, colour) = when { degrees < 5 -> "cold" to BLUE degrees < 23 -> "mild" to ORANGE else -> "hot" to RED } }

Page 30: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

val (description, colour) = when { degrees < 5 -> "cold" to BLUE degrees < 23 -> "mild" to ORANGE else -> "hot" to RED}

String description;Colour colour; if (degrees < 5) { description = "cold"; colour = BLUE; } else if (degrees < 23) { description = "mild"; colour = ORANGE; } else { description = "hot"; colour = RED; }

Page 31: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

safe

Page 32: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Billion Dollar Mistake

Page 33: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Modern approach: to make NPE

compile-time error, not run-time error

Page 34: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Nullable types in Kotlinval s1: String = "always not null" val s2: String? = null

s1.length ✓

✗s2.length

"can be null or non-null"

null ✗

Page 35: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

val s: String?

if (s != null) { s.length}

Dealing with Nullable Types

Page 36: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

s?.length

val s: String?

Dealing with Nullable Types

Page 37: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

val length = if (s != null) s.length else null

val s: String?

Nullability operators

val length = s?.length

Page 38: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

val length = if (s != null) s.length else 0

val s: String?

Nullability operators

val length = s?.length ?: 0

Page 39: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

val s: String?

if (s == null) fail() s.length

Control-flow analysis

Page 40: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

val s: String?

Making NPE explicit

s!!throws NPE if s is null

s!!.length

Page 41: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Nullable Types Under the Hood

No performance overhead

@Nullable, @NotNull annotations

Page 42: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

class Optional<T>(val value: T) { fun isPresent() = value != null fun get() = value ?: throw NoSuchElementException("No value present") }

Nullable types ≠ Optional

Page 43: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Annotate your Java types in Kotlin

Type behaves like regular Java type

@Nullable

@NotNull Type

Type?

Type

Type

@ParametersAreNonnullByDefault

@MyNonnullApiType/Type?

Page 44: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

expressive

Page 45: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

you can avoid any repetition

you can make the code look nicer

you can create API looking like DSL

expressive

Page 46: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Kotlin Coroutines

Page 47: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Kotlin Coroutines

• the key new feature in Kotlin 1.1 • simplify asynchronous programming

Page 48: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Coroutine

a main routine and a subroutine vs coroutines, which call on each other

• the term from 1960s • was used in “The Art of Computer Programming”

by Donald Knuth

Page 49: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Motivation: async/await

Page 50: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Motivationtime consuming

operation

val image = loadImage(url) setImage(image)

Page 51: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Solution 1: callbacks

loadImageAsync().whenComplete { image -> runOnUiThread { setImage(image) } }

Page 52: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Solution 2: async/await

async(UI) { val image = loadImageAsync(url).await() setImage(image) }

Page 53: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

val image = loadImageAsync(url).await() setImage(image)

val image = loadImage(url) setImage(image)

No callbacks!

Page 54: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

C# wayasync Task ProcessImage(String url) { var image = await LoadImage(url); SetImage(image); }

Kotlin wayfun processImage() = async { val image = loadImageAsync().await() setImage(image) }

Page 55: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

coroutines

async/await

Language

Library

async/await - functions defined in the standard library

Page 56: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Threads & Coroutines

Page 57: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Coroutine is similar to a thread

Thread:• a sequence of instructionsMultiple threads:• can be executed concurrently• share resources such as memory

Coroutine:

coroutines:

Page 58: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Coroutine = “lightweight thread”

Thread

Coroutine

Page 59: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Coroutine

computation that can be suspended

computationthread

Page 60: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Coroutine

computation that can be suspended

Page 61: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Why suspend?

Page 62: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Asynchronous computations:

how?

Page 63: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

New thread for every computation

simple & straightforward, but too expensive

Page 64: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Executor

• fixed number of threads

• adding tasks • but: difficult to manage

dependencies

Page 65: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Example: waiting for two asynchronous computations to complete

Page 66: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

thenCombine / zip

CompletableFutures / RxJava: managing dependencies

the rest of computation in a callback

Page 67: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Stepping back: what’s wrong with blocking thread?

idle

too expensive

Page 68: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Stepping back: what’s wrong with blocking UI thread?

blocked

user is blocked

UI

Page 69: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

1

2

3

• computation that can be suspended

• thread is not blocked!

Coroutines

Page 70: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

1

2

3

• computation that can be suspended

• thread is not blocked!

UI

UI

UI

Coroutines

Page 71: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

suspend functioncomputation that can be suspended

Page 72: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

fun loadImageAsync() = async { /* do the work */ }

fun processImage() = async { val image = loadImageAsync().await() setImage(image) }

Back to image example

Page 73: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

async starts new computationfun loadImageAsync() = async { /* do the work */ }

loadImageAsync

Page 74: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

suspending callfun processImage() = async {

val image = loadImageAsync().await() setImage(image) }

await suspends computation

Page 75: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

fun loadImageAsync(): Deferred<Image> = async { /* do the work */ }

interface Deferred<out T> { suspend fun await(): T }

await is a suspend function

Page 76: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

await suspends computationfun processImage() = async {

val image = loadImageAsync().await() setImage(image) }

Page 77: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

fun processImage() = async { val deferred = loadImageAsync() val image = deferred.await() setImage(image) }

await suspends computation

Page 78: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

await suspends computation

processImage

loadImageAsync

1

fun processImage() = async { val deferred = loadImageAsync() val image = deferred.await() setImage(image) }

Page 79: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

await suspends computation

processImage

loadImageAsync

1loadImageAsync

processImage

2

await

fun processImage() = async { val deferred = loadImageAsync() val image = deferred.await() setImage(image) }

Page 80: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

await suspends computation

loadImageAsync

processImage

2

loadImageAsync

processImage

3

…and continues it when result is ready

fun processImage() = async { val deferred = loadImageAsync() val image = deferred.await() setImage(image) }

Page 81: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

await suspends computation

loadImageAsync

processImage

2

loadImageAsync

processImage

3

…and continues it when result is ready

On which thread?

Page 82: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Q: On which thread can the coroutine be continued?A: You specify that.

Page 83: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Continue in any thread from thread pool

async(CommonPool) { ...}

1

2

3 3

Page 84: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Continue in the UI thread1

2

3

async(UI) { ... }

UI

UI

UI

Page 85: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Custom executor

async(CustomContext) { ...}

You can launch coroutine in the custom executor

Page 86: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

1 2 3

fun overlay(first: Image, second: Image): Image

fun overlayAsync() = async(CommonPool) { val first = loadImageAsync("green") val second = loadImageAsync("red") overlay(first.await(), second.await())}

Two asynchronous computations

overlayAsync

overlayAsync

overlayAsync

Page 87: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Programming with suspend functions

Page 88: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Q: Can I define my custom suspend functions?A: Yes.

Page 89: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Example: simple consecutive logicfun login(credentials: Credentials): UserID fun loadUserData(userID: UserID): UserData fun showData(data: UserData)

fun showUserInfo(cred: Credentials) { val userID = login(credentials) val userData = loadUserData(userID) showData(userData) }

Page 90: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Rewrite with async/awaitfun login(credentials: Credentials): Deferred<UserID> fun loadUserData(userID: UserID): Deferred<UserData> fun showData(data: UserData)

fun showUserInfo(credentials: Credentials) = async(CommonPool) { val userID = login(credentials).await() val userData = loadUserData(userID).await() showData(userData) }

Page 91: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Rewrite with suspend functionssuspend fun login(credentials: Credentials): UserID suspend fun loadUserData(userID: UserID): UserData fun showData(data: UserData)

suspend fun showUserInfo(credentials: Credentials) { val userID = login(credentials) val userData = loadUserData(userID) showData(userData) }

Page 92: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

RxJava / CompletableFuture vs

Coroutines

Page 93: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Rewrite with CompletableFuturefun loginAsync(credentials: Credentials): CompletableFuture<UserID> fun loadUserDataAsync(userID: UserID): CompletableFuture<UserData> fun showData(data: UserData)

fun showUserInfo(credentials: Credentials) { loginAsync(credentials) .thenCompose { loadUserDataAsync(it) } .thenAccept { showData(it) } }

Page 94: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Rewrite with RxJavafun login(credentials: Credentials): Single<UserID> fun loadUserData(userID: UserID): Single<UserData> fun showData(data: UserData)

fun showUserInfo(credentials: Credentials) { login(credentials) .flatMap { loadUserData(it) } .doOnSuccess { showData(it) } .subscribe() }

Page 95: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

“Observables are great, but in many cases they’re kind of overkill.”

somewhere on the Internet

Page 96: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Kotlincoroutines

Reactive Streams

RxJava & coroutines

Page 97: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions
Page 98: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

coroutines

async/await

Language

Library

channels

actors

kotlinx.coroutines

yield

Page 99: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Experimental status of Coroutines

• We want the community to try it! • Migration aids will be provided • Old code will continue to work

via the support library

Page 100: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

kotlinx.coroutines

• https://github.com/Kotlin/kotlinx.coroutines/

• Guide to kotlinx.coroutines by example

• by Roman Elizarov (@relizarov)

Page 101: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions
Page 102: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

kotlinlang.org

Page 103: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Gradle & Kotlin

Writing Gradle build scripts and plugins in Kotlin

Page 104: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

try.kotlinlang.org

Page 105: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Kotlin Koans

Page 106: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions
Page 107: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

• Basics ◦ j2k converter ◦ functions, variables ◦ control structures (if, when, for, while) ◦ try/catch & exceptions

• Object-oriented programming ◦ classes, interfaces ◦ data classes ◦ objects ◦ class objects ◦ class delegation ◦ extension functions

• Functional programming ◦ lambdas ◦ working with collections in a functional style (filter, map, etc.) ◦ inlining ◦ difference between collections & sequences ◦ anonymous functions (return in lambda vs return in anonymous function)

• Nullability ◦ nullable types ◦ special operators (?., ?:, !!) ◦ let function ◦ lateinit modifier

• Java interoperability ◦ Kotlin type hierarchy (Nothing, Any?, etc.) ◦ correspondence between Kotlin and Java types ◦ platform types for nullability (Int!) ◦ collections hierarchy (read-only / mutable list vs java.util.List) ◦ platform types for collections ((Mutable)List<Int!>) ◦ annotations (@JvmName, @JvmOverloads, @Throws, @JvmStatic)

• Asynchronous programming ◦ async/await ◦ coroutines & suspend functions

• Building DSLs ◦ lambdas with receiver ◦ with, apply, run functions ◦ builders pattern (HTML builders)

• Conventions ◦ operator overloading (a + b, a < b) ◦ others (e in set, map[key], first..last, val (a, b) = pair) ◦ delegated properties (val prop by lazy { ... })

• Generics ◦ reified type parameters ◦ basic intro to covariance / contravariance

1. The Kotlin code is compiled to:

a) Java source code

b) Java byte code

2. Does the following code compile? If not, w

hat’s the error?

var

strin

g = 1

str

ing =

"a"

3. Does the following code compile? If not, w

hat’s the error?

val

langu

ages =

listO

f("Jav

a")

lan

guages

.add("

Kotlin

")

4. What will be printed?

_____

______

_____

p

rintln

(listO

f('a',

'b',

'c').j

oinToS

tring(

separ

ator =

"", p

refix

= "(",

postf

ix = "

)"))

5. What will be printed (a or b)?

f

un foo

(): St

ring {

pri

ntln("

Calcul

ating

foo...

")

ret

urn "f

oo"

}

f

un mai

n(args

: Arra

y<Stri

ng>) {

pri

ntln("

First

${foo(

)}, se

cond $

{foo()

}")

}

a)

Calcul

ating

foo...

First

foo, s

econd

foo

b)

Calcul

ating

foo...

Calcul

ating

foo...

First

foo, s

econd

foo

6. What will be printed?

pri

ntln("

Kotlin

" in "

Java".

."Scal

a")

_

______

______

___

pri

ntln("

Kotlin

" in s

etOf("

Java",

"Scal

a")) _

______

______

___

7. Rewrite the following Java code into Kotlin:

for

(char

c = '

0'; c

< '9';

c++)

{

Sy

stem.o

ut.pri

nt(c);

}

___________________________________________

___________________________________________

___________________________________________

Workshop tomorrow

Page 108: You can do better with Kotlin...Android Studio is based on IntelliJ IDEA just another library for your app rxjava-2.1.2 kotlin-stdlib-1.1.4 6315 10212 No Kotlin SDK …just JDK + extensions

Have a nice Kotlin!