Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin — A deeper look

Kotlin — A deeper look

https://www.meetup.com/kotlin-berlin/events/240133229/

Let's see how Kotlin pulls of his tricks and still manages to keep interoperability with Java.

Article: https://hackernoon.com/kotlin-a-deeper-look-8569d4da36f
Animated slides (Keynote): https://goo.gl/f8xspX

Sebastiano Gottardo

June 15, 2017
Tweet

More Decks by Sebastiano Gottardo

Other Decks in Technology

Transcript

  1. Kotlin — A deeper look

    View full-size slide

  2. 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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. /**
    * 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 }

    View full-size slide

  19. /**
    * 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
    }
    }

    View full-size slide

  20. 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

    View full-size slide

  21. 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;
    }
    }

    View full-size slide

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

    View full-size slide

  23. /**
    * 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()

    View full-size slide

  24. 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()

    View full-size slide

  25. 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

    View full-size slide

  26. /**
    * 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)

    View full-size slide

  27. 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)

    View full-size slide

  28. 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

    View full-size slide

  29. /**
    * 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()

    View full-size slide

  30. 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()

    View full-size slide

  31. 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

    View full-size slide

  32. Standard functions — Cheat sheet
    ← We can thank this guy

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide