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

How Kotlin implements features Java doesn't have

How Kotlin implements features Java doesn't have

CA.kt #1
TwitterID変えました。@lvla0805 -> @MoyuruAizawa

Moyuru Aizawa

June 15, 2017
Tweet

More Decks by Moyuru Aizawa

Other Decks in Programming

Transcript

  1. Data Classes Sealed Classes Extension Functions Inline Functions Default Arguments

    Named Arguments Delegation Properties Auto Casts Type Alias Covariance Null Safety Elvis Operator Spread Operator Operator Overloading Type Inference DSL Support Type-Safe Builders Coroutines String Interpolation Rages Map Array Syntax Destructuring KDoc
  2. public final nonNullInt(I)V L0 LINENUMBER 7 L0 ILOAD 1 INVOKESTATIC

    java/lang/String.valueOf (I)Ljava/lang/String; POP … NonNull: Bytecode
  3. public final nonNullInt(I)V L0 LINENUMBER 7 L0 ILOAD 1 INVOKESTATIC

    java/lang/String.valueOf (I)Ljava/lang/String; POP … NonNull: Bytecode
  4. public final nullableInt(Ljava/lang/Integer;)V @Lorg/jetbrains/annotations/Nullable;() // invisible, parameter 0 L0 LINENUMBER

    7 L0 ALOAD 1 DUP IFNULL L1 INVOKEVIRTUAL java/lang/Integer.intValue ()I INVOKESTATIC java/lang/String.valueOf (I)Ljava/lang/String; POP GOTO L2 L1 POP … Nullable: Bytecode
  5. public final nullableInt(Ljava/lang/Integer;)V @Lorg/jetbrains/annotations/Nullable;() // invisible, parameter 0 L0 LINENUMBER

    7 L0 ALOAD 1 DUP IFNULL L1 INVOKEVIRTUAL java/lang/Integer.intValue ()I INVOKESTATIC java/lang/String.valueOf (I)Ljava/lang/String; POP GOTO L2 L1 POP … Nullable: Bytecode
  6. public final nullableInt(Ljava/lang/Integer;)V @Lorg/jetbrains/annotations/Nullable;() // invisible, parameter 0 L0 LINENUMBER

    7 L0 ALOAD 1 DUP IFNULL L1 INVOKEVIRTUAL java/lang/Integer.intValue ()I INVOKESTATIC java/lang/String.valueOf (I)Ljava/lang/String; POP GOTO L2 L1 POP … Nullable: Bytecode
  7. public final void nullableInt(@Nullable Integer number) { if(number != null)

    { String.valueOf(number.intValue()); } } Nullable: Java
  8. public final nonNullString(Ljava/lang/String;)V @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0 L0 ALOAD

    1 LDC "string" INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull (Ljava/lang/Object;Ljava/lang/String;)V L1 LINENUMBER 7 L1 ALOAD 1 INVOKEVIRTUAL java/lang/String.length ()I POP … NonNull: Bytecode
  9. public final nonNullString(Ljava/lang/String;)V @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0 L0 ALOAD

    1 LDC "string" INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull (Ljava/lang/Object;Ljava/lang/String;)V L1 LINENUMBER 7 L1 ALOAD 1 INVOKEVIRTUAL java/lang/String.length ()I POP … NonNull
  10. public final nonNullString(Ljava/lang/String;)V @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0 L0 ALOAD

    1 LDC "string" INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull (Ljava/lang/Object;Ljava/lang/String;)V L1 LINENUMBER 7 L1 ALOAD 1 INVOKEVIRTUAL java/lang/String.length ()I POP … NonNull: Bytecode
  11. private final static nullableString(Ljava/lang/String;)V L0 LINENUMBER 2 L0 ALOAD 0

    DUP IFNULL L1 INVOKEVIRTUAL java/lang/String.length ()I POP GOTO L2 L1 POP L2 L3 LINENUMBER 3 L3 RETURN … Nullable: Bytecode
  12. Conclusion of NonNull/Nullable Primitive NonNull -> Primitive Primitive Nullable ->

    BoxType ClassType NonNull -> ClassType, Check Null ClassType Nullable -> ClassType Safe Call -> IFNULL
  13. public final class StringExtensionKt { public static final void println(@NotNull

    String $receiver) { Intrinsics.checkParameterIsNotNull($receiver, "$receiver"); System.out.println($receiver); } } Package Level Extension Function: Java
  14. Package Level Extension Function Public Package Level Extension Function ->

    Public Static Method Private Package Level Extension Function -> Private Static Method
  15. Scope of Package Level Extension Public Package Level Extension Function

    -> Anywhere Private Package Level Extension Function -> Same file
  16. // in Bar.kt private fun Foo.doSomething() { } class Foo

    { init { doSomething() } } Private Package Level Extension Function: Kotlin
  17. public final class FooKt { private final static doSomething(LFoo;)V …

    public final static synthetic access$doSomething(LFoo;)V @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0 L0 LINENUMBER 1 L0 ALOAD 0 INVOKESTATIC FooKt.doSomething (LFoo;)V RETURN L1 LOCALVARIABLE $receiver LFoo; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 Private Package Level Extension Function: Bytecode
  18. public final class FooKt { private final static doSomething(LFoo;)V …

    public final static synthetic access$doSomething(LFoo;)V @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0 L0 LINENUMBER 1 L0 ALOAD 0 INVOKESTATIC FooKt.doSomething (LFoo;)V RETURN L1 LOCALVARIABLE $receiver LFoo; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 Private Package Level Extension Function: Bytecode
  19. public final class FooKt { private final static doSomething(LFoo;)V …

    public final static synthetic access$doSomething(LFoo;)V @Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0 L0 LINENUMBER 1 L0 ALOAD 0 INVOKESTATIC FooKt.doSomething (LFoo;)V RETURN L1 LOCALVARIABLE $receiver LFoo; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 Private Package Level Extension Function: Bytecode
  20. Conclusion of Extension Function Package Level Extension Function -> Static

    Method Class Level Extension Function -> Method Call Private Package Level Extension Function from inside a class of same file -> Private Static Method and Accessor Method
  21. fun foo(one: String, two: String, three: String) { } foo(three

    = "tres", one = "uno", two = "dos") Named Argument: Kotlin
  22. LDC "tres" LDC "uno" LDC "dos" ASTORE 1 ASTORE 2

    ASTORE 3 ALOAD 2 ALOAD 1 ALOAD 3 INVOKEVIRTUAL Foo.foo (Ljava/lang/String;Ljava/lang/String;Ljava/lang/ String;)V Named Argument: Bytecode
  23. LDC "tres" LDC "uno" LDC "dos" ASTORE 1 ASTORE 2

    ASTORE 3 ALOAD 2 ALOAD 1 ALOAD 3 INVOKEVIRTUAL Foo.foo (Ljava/lang/String;Ljava/lang/String;Ljava/lang/ String;)V Named Argument: Bytecode
  24. LDC "tres" LDC "uno" LDC "dos" ASTORE 1 ASTORE 2

    ASTORE 3 ALOAD 2 ALOAD 1 ALOAD 3 INVOKEVIRTUAL Foo.foo (Ljava/lang/String;Ljava/lang/String;Ljava/lang/ String;)V Named Argument: Bytecode
  25. LDC "tres" LDC "uno" LDC "dos" ASTORE 1 ASTORE 2

    ASTORE 3 ALOAD 2 ALOAD 1 ALOAD 3 INVOKEVIRTUAL Foo.foo (Ljava/lang/String;Ljava/lang/String;Ljava/lang/ String;)V Named Argument: Bytecode
  26. String var1 = "dos"; String var2 = "uno"; String var3

    = "tres"; this.foo(var2, var1, var3); Named Argument: Java
  27. fun foo(one: String = “one", two: String = “two", three:

    String = "three") { } foo() foo("uno") foo("uno", "dos") foo("uno", "dos", "tres") foo(two = "dos") Default Argument: Kotlin
  28. fun foo(one: String = “one", two: String = “two", three:

    String = "three") { } foo(two = "dos") Default Argument: Kotlin
  29. ‣ public static void foo(String, String, String) ‣ public static

    void foo$default(String, String, String, int, Object) Default Argument: Java
  30. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java
  31. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java
  32. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java
  33. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java
  34. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java
  35. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java
  36. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java
  37. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java 0 1 1
  38. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java 101
  39. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java
  40. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java var1: null var2: “dos” var3: null
  41. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java 101 001 101
 and 001 001 var1: null var2: “dos” var3: null
  42. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java 101 001 101
 and 001 001 var1: “one” var2: “dos” var3: null
  43. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java 101 010 101
 and 010 000 var1: “one” var2: “dos” var3: null
  44. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java 101 100 101
 and 100 100 var1: “one” var2: “dos” var3: null
  45. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java 101 100 101
 and 100 100 var1: “one” var2: “dos” var3: “three”
  46. public static void foo$default( String var1, String var2, String var3,

    int var4, Object var5) { if((var4 & 1) != 0) { var1 = "one"; } if((var4 & 2) != 0) { var2 = "two"; } if((var4 & 4) != 0) { var3 = "three"; } foo(var1, var2, var3); } foo$default((String)null, "dos", (String)null, 5, (Object)null); Default Argument: Java “one”“dos”“three”