Slide 1

Slide 1 text

Kotlin — A deeper look

Slide 2

Slide 2 text

03 3b 84 00 01 1a 05 68 3b a7 ff f9
 03 3b 84 00 01 1a 05 68 3b a7 ff f9
 03 3b 84 00 01 1a 05 68 3b a7 ff f9

Slide 3

Slide 3 text

We already love Kotlin Data classes, extension functions, immutability, … Modern and thriving Interoperable with Java

Slide 4

Slide 4 text

We already love Kotlin Data classes, extension functions, immutability, … Modern and thriving Interoperable with Java

Slide 5

Slide 5 text

We already love Kotlin Data classes, extension functions, immutability, … Modern and thriving Interoperable with Java

Slide 6

Slide 6 text

We already love Kotlin Data classes, extension functions, immutability, … Modern and thriving Interoperable with Java

Slide 7

Slide 7 text

We already love Kotlin Data classes, extension functions, immutability, … Modern and thriving Interoperable with Java

Slide 8

Slide 8 text

How… … does the Kotlin compiler produce Java-compatible bytecode? … can we trust the compiler? … much overhead does the interoperability introduce?

Slide 9

Slide 9 text

How… … does the Kotlin compiler produce compatible bytecode? … can we trust the compiler? … much overhead does the interoperability introduce?

Slide 10

Slide 10 text

How… … does the Kotlin compiler produce compatible bytecode? … can we trust the compiler? … much overhead does the interoperability introduce?

Slide 11

Slide 11 text

How… … does the Kotlin compiler produce compatible bytecode? … can we trust the compiler? … much overhead does the interoperability introduce?

Slide 12

Slide 12 text

How… … does the Kotlin compiler produce compatible bytecode? … can we trust the compiler? … much overhead does the interoperability introduce?

Slide 13

Slide 13 text

Kotlin Standard Library Extension functions String/char sequences utilities High-order functions

Slide 14

Slide 14 text

Kotlin Standard Library Extension functions String/char sequences utilities High-order functions

Slide 15

Slide 15 text

Kotlin Standard Library Extension functions String/char sequences utilities High-order functions

Slide 16

Slide 16 text

Kotlin Standard Library Extension functions String/char sequences utilities High-order functions

Slide 17

Slide 17 text

Kotlin Standard Library Extension functions String/char sequences utilities High-order functions Apply — With — Let — Run

Slide 18

Slide 18 text

/** * Calls the specified function [block] with `this` * value as its receiver and returns `this` value. */ @kotlin.internal.InlineOnly public inline fun T.apply(block: T.() -> Unit): T { block(); return this }

Slide 19

Slide 19 text

/** * Calls the specified function [block] with `this` * value as its receiver and returns `this` value. */ @kotlin.internal.InlineOnly public inline fun T.apply(block: T.() -> Unit): T { block(); return this } enum class Orientation { VERTICAL, HORIZONTAL } class LayoutStyle { var orientation = Orientation.HORIZONTAL init { if (System.currentTimeMillis() < 1) { main() } } fun main() { val layout = LayoutStyle().apply { orientation = Orientation.VERTICAL } layout.orientation } }

Slide 20

Slide 20 text

NEW kotlindeepdive/LayoutStyle DUP INVOKESPECIAL kotlindeepdive/LayoutStyle. ()V ASTORE 2 ALOAD 2 ASTORE 3 ALOAD 3 GETSTATIC kotlindeepdive/Orientation.VERTICAL : Lkotlindeepdive/Orientation; INVOKEVIRTUAL kotlindeepdive/LayoutStyle.setOrientation (Lkotlindeepdive/Orientation;)V ALOAD 2 ASTORE 1

Slide 21

Slide 21 text

enum OrientationJ { VERTICAL, HORIZONTAL; } class LayoutStyle { private Orientation orientation = HORIZONTAL; public Orientation getOrientation() { return orientation; } public LayoutStyle() { if (System.currentTimeMillis() < 1) { main(); } } public void setOrientation(Orientation orientation) { this.orientation = orientation; } public Orientation main() { LayoutStyle layout = new LayoutStyle(); layout.setOrientation(VERTICAL); return layout.orientation; } }

Slide 22

Slide 22 text

NEW kotlindeepdive/LayoutStyle DUP ASTORE 1 ALOAD 1 GETSTATIC kotlindeepdive/Orientation.VERTICAL : kotlindeepdive/Orientation; INVOKEVIRTUAL kotlindeepdive/LayoutStyle.setOrientation (kotlindeepdive/Orientation;)V

Slide 23

Slide 23 text

/** * Calls the specified function [block] with the given [receiver] as its * receiver and returns its result. */ @kotlin.internal.InlineOnly public inline fun with(receiver: T, block: T.() -> R): R = receiver.block()

Slide 24

Slide 24 text

enum class Orientation { VERTICAL, HORIZONTAL } class LayoutStyle { var orientation = HORIZONTAL } object SharedState { val previousOrientation = VERTICAL } fun main() { val layout = with(SharedState) { LayoutStyle().apply { orientation = previousOrientation } } } /** * Calls the specified function [block] with the given [receiver] as its * receiver and returns its result. */ @kotlin.internal.InlineOnly public inline fun with(receiver: T, block: T.() -> R): R = receiver.block()

Slide 25

Slide 25 text

GETSTATIC kotlindeepdive/SharedState.INSTANCE : Lkotlindeepdive/SharedState; ASTORE 1 ALOAD 1 ASTORE 2 NEW kotlindeepdive/LayoutStyle DUP INVOKESPECIAL kotlindeepdive/LayoutStyle. ()V ASTORE 3 ALOAD 3 ASTORE 4 ALOAD 4 ALOAD 2 INVOKEVIRTUAL kotlindeepdive/ SharedState.getPreviousOrientation ()Lkotlindeepdive/ Orientation; INVOKEVIRTUAL kotlindeepdive/LayoutStyle.setOrientation (Lkotlindeepdive/Orientation;)V ALOAD 3 ASTORE 0 RETURN

Slide 26

Slide 26 text

/** * Calls the specified function [block] with `this` value as its * argument and returns its result. */ @kotlin.internal.InlineOnly public inline fun T.let(block: (T) -> R): R = block(this)

Slide 27

Slide 27 text

enum class Orientation { VERTICAL, HORIZONTAL } class LayoutStyle { var orientation = HORIZONTAL } object SharedState { val previousOrientation: Orientation? = VERTICAL } fun main() { val layout = LayoutStyle() SharedState.previousOrientation ?.let { layout.orientation = it } } /** * Calls the specified function [block] with `this` value as its * argument and returns its result. */ @kotlin.internal.InlineOnly public inline fun T.let(block: (T) -> R): R = block(this)

Slide 28

Slide 28 text

NEW kotlindeepdive/let/LayoutStyle DUP INVOKESPECIAL kotlindeepdive/let/LayoutStyle. ()V GETSTATIC kotlindeepdive/let/SharedState.INSTANCE : Lkotlindeepdive/let/SharedState; INVOKEVIRTUAL kotlindeepdive/let/ SharedState.getPreviousOrientation ()Lkotlindeepdive/ let/Orientation; DUP IFNULL L2 ASTORE 1 ALOAD 1 ASTORE 2 ALOAD 0 ALOAD 2 INVOKEVIRTUAL kotlindeepdive/let/ LayoutStyle.setOrientation (Lkotlindeepdive/let/ Orientation;)V GOTO L9 L2 POP L9 RETURN

Slide 29

Slide 29 text

/** * Calls the specified function [block] with `this` value as * its receiver and returns its result. */ @kotlin.internal.InlineOnly public inline fun T.run(block: T.() -> R): R = block()

Slide 30

Slide 30 text

enum class Orientation { VERTICAL, HORIZONTAL } class LayoutStyle { var orientation = HORIZONTAL } object SharedState { val previousOrientation = VERTICAL } fun main() { val layout = LayoutStyle().run { orientation = SharedState.previousOrientation this } } /** * Calls the specified function [block] with `this` value as * its receiver and returns its result. */ @kotlin.internal.InlineOnly public inline fun T.run(block: T.() -> R): R = block()

Slide 31

Slide 31 text

NEW kotlindeepdive/LayoutStyle DUP INVOKESPECIAL kotlindeepdive/LayoutStyle. ()V ASTORE 0 ALOAD 0 ASTORE 1 ALOAD 1 ASTORE 2 ALOAD 2 GETSTATIC kotlindeepdive/SharedState.INSTANCE : Lkotlindeepdive/SharedState; INVOKEVIRTUAL kotlindeepdive/ SharedState.getPreviousOrientation ()Lkotlindeepdive/ Orientation; INVOKEVIRTUAL kotlindeepdive/LayoutStyle.setOrientation (Lkotlindeepdive/Orientation;)V RETURN

Slide 32

Slide 32 text

Standard functions — Cheat sheet ← We can thank this guy

Slide 33

Slide 33 text

Superfluous for production environments ASTORE / ALOAD Inserted by the compiler to allow debugging Can be stripped with tools

Slide 34

Slide 34 text

ASTORE / ALOAD Inserted by the compiler to allow debugging Superfluous for production environments Can be stripped with tools

Slide 35

Slide 35 text

ASTORE / ALOAD Inserted by the compiler to allow debugging Superfluous for production environments Can be stripped with tools

Slide 36

Slide 36 text

Inserted by the compiler to allow debugging Superfluous for production environments ASTORE / ALOAD Can be stripped with tools

Slide 37

Slide 37 text

Inserted by the compiler to allow debugging Superfluous for production environments ASTORE / ALOAD Can be stripped with tools ProGuard!

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Thank you! @rotxed http://bit.ly/kotlin-deeper-look-article