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. 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
  2. How… … does the Kotlin compiler produce Java-compatible bytecode? …

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

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

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

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

    can we trust the compiler? … much overhead does the interoperability introduce?
  7. /** * Calls the specified function [block] with `this` *

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

    value as its receiver and returns `this` value. */ @kotlin.internal.InlineOnly public inline fun <T> 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 } }
  9. NEW kotlindeepdive/LayoutStyle DUP INVOKESPECIAL kotlindeepdive/LayoutStyle.<init> ()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
  10. 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; } }
  11. NEW kotlindeepdive/LayoutStyle DUP ASTORE 1 ALOAD 1 GETSTATIC kotlindeepdive/Orientation.VERTICAL :

    kotlindeepdive/Orientation; INVOKEVIRTUAL kotlindeepdive/LayoutStyle.setOrientation (kotlindeepdive/Orientation;)V
  12. /** * Calls the specified function [block] with the given

    [receiver] as its * receiver and returns its result. */ @kotlin.internal.InlineOnly public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
  13. 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 <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
  14. GETSTATIC kotlindeepdive/SharedState.INSTANCE : Lkotlindeepdive/SharedState; ASTORE 1 ALOAD 1 ASTORE 2

    NEW kotlindeepdive/LayoutStyle DUP INVOKESPECIAL kotlindeepdive/LayoutStyle.<init> ()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
  15. /** * Calls the specified function [block] with `this` value

    as its * argument and returns its result. */ @kotlin.internal.InlineOnly public inline fun <T, R> T.let(block: (T) -> R): R = block(this)
  16. 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, R> T.let(block: (T) -> R): R = block(this)
  17. NEW kotlindeepdive/let/LayoutStyle DUP INVOKESPECIAL kotlindeepdive/let/LayoutStyle.<init> ()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
  18. /** * Calls the specified function [block] with `this` value

    as * its receiver and returns its result. */ @kotlin.internal.InlineOnly public inline fun <T, R> T.run(block: T.() -> R): R = block()
  19. 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, R> T.run(block: T.() -> R): R = block()
  20. NEW kotlindeepdive/LayoutStyle DUP INVOKESPECIAL kotlindeepdive/LayoutStyle.<init> ()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
  21. Superfluous for production environments ASTORE / ALOAD Inserted by the

    compiler to allow debugging Can be stripped with tools
  22. ASTORE / ALOAD Inserted by the compiler to allow debugging

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

    Superfluous for production environments Can be stripped with tools
  24. Inserted by the compiler to allow debugging Superfluous for production

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

    environments ASTORE / ALOAD Can be stripped with tools ProGuard!