Slide 1

Slide 1 text

Typical Kotlin Braun Márton Szabolcs zsmb.co zsmb13 [email protected]

Slide 2

Slide 2 text

Hello

Slide 3

Slide 3 text

Hello class

Slide 4

Slide 4 text

Hello class

Slide 5

Slide 5 text

Hello class class List

Slide 6

Slide 6 text

Any The root of the hierarchy public open class Any { public open operator fun equals(other: Any?): Boolean public open fun hashCode(): Int public open fun toString(): String }

Slide 7

Slide 7 text

Any The root of the hierarchy

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

val maybeString: String? = null val definitelyString = maybeString ?: "replacement"

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Unit public object Unit { override fun toString() = "kotlin.Unit" } The type with only one value

Slide 21

Slide 21 text

val noResult: Task< > Unit Unit The type with only one value fun () { // Empty! } empty() { // Empty! } Task< > noResult; Unit void Void : empty

Slide 22

Slide 22 text

Unit Unit The type with only one value void Void

Slide 23

Slide 23 text

Nothing A value that never exists public class Nothing private constructor()

Slide 24

Slide 24 text

Nothing A value that never exists fun loopy(): Nothing { while (true) { println("Loop!") } } fun exceptional(): Nothing { throw IllegalStateException() }

Slide 25

Slide 25 text

Nothing A value that never exists

Slide 26

Slide 26 text

Nothing The bottom type

Slide 27

Slide 27 text

Nothing The bottom type fun processData(data: List) { // Use data } fun main(args: Array) { val data: Nothing = loopy() processData(data) }

Slide 28

Slide 28 text

Nothing Elvis’ best friend fun calculate(someParam: Int?) { val x = someParam ?: exceptional() val y = x * 2 println(y) }

Slide 29

Slide 29 text

Nothing Elvis’ best friend

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

exceptional() Nothing Elvis’ best friend fun calculate(someParam: Int?) { val x = someParam ?: val y = x * 2 println(y) }

Slide 34

Slide 34 text

Nothing Elvis’ best friend throw IllegalArgumentException("!") fun calculate(someParam: Int?) { val x = someParam ?: val y = x * 2 println(y) }

Slide 35

Slide 35 text

* 2 someParam ?: return fun calculate(someParam: Int?) { val y = println(y) } Nothing Elvis’ best friend x val x =

Slide 36

Slide 36 text

someParam ?: return * 2 fun calculate(someParam: Int?) { val y = println(y) } someParam Nothing Elvis’ best friend

Slide 37

Slide 37 text

Nothing? Nothing.

Slide 38

Slide 38 text

Nothing? Nothing.

Slide 39

Slide 39 text

Nothing? Nothing. var x = null x = "foo" Type mismatch. Required: Nothing? Found: String

Slide 40

Slide 40 text

Nothing Edge cases fun oddity() { throw return return throw return }

Slide 41

Slide 41 text

Resources • Illustration style credit  http://natpryce.com/articles/000818.html • More about Nothing  http://oneeyedmen.com/nothing-can-save-us.html  https://blog.kotlin-academy.com/kotlins-nothing-its- usefulness-in-generics-5076a6a457f7

Slide 42

Slide 42 text

Questions? zsmb.co zsmb13 [email protected]