How Kotlin implements features Java doesn't have

How Kotlin implements features Java doesn't have

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

5f533179da1c82722252cbcb93e7356f?s=128

Moyuru Aizawa

June 15, 2017
Tweet

Transcript

  1. 3.

    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. 4.
  3. 9.

    public final nonNullInt(I)V L0 LINENUMBER 7 L0 ILOAD 1 INVOKESTATIC

    java/lang/String.valueOf (I)Ljava/lang/String; POP … NonNull: Bytecode
  4. 10.

    public final nonNullInt(I)V L0 LINENUMBER 7 L0 ILOAD 1 INVOKESTATIC

    java/lang/String.valueOf (I)Ljava/lang/String; POP … NonNull: Bytecode
  5. 13.

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

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

    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
  8. 16.

    public final void nullableInt(@Nullable Integer number) { if(number != null)

    { String.valueOf(number.intValue()); } } Nullable: Java
  9. 18.

    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
  10. 19.

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

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

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

    Conclusion of NonNull/Nullable Primitive NonNull -> Primitive Primitive Nullable ->

    BoxType ClassType NonNull -> ClassType, Check Null ClassType Nullable -> ClassType Safe Call -> IFNULL
  14. 35.

    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
  15. 37.
  16. 41.

    Package Level Extension Function Public Package Level Extension Function ->

    Public Static Method Private Package Level Extension Function -> Private Static Method
  17. 42.

    Scope of Package Level Extension Public Package Level Extension Function

    -> Anywhere Private Package Level Extension Function -> Same file
  18. 44.

    // in Bar.kt private fun Foo.doSomething() { } class Foo

    { init { doSomething() } } Private Package Level Extension Function: Kotlin
  19. 45.

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

    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
  21. 47.

    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
  22. 48.

    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
  23. 50.

    fun foo(one: String, two: String, three: String) { } foo(three

    = "tres", one = "uno", two = "dos") Named Argument: Kotlin
  24. 51.

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

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

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

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

    String var1 = "dos"; String var2 = "uno"; String var3

    = "tres"; this.foo(var2, var1, var3); Named Argument: Java
  29. 56.

    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
  30. 57.

    fun foo(one: String = “one", two: String = “two", three:

    String = "three") { } foo(two = "dos") Default Argument: Kotlin
  31. 58.

    ‣ public static void foo(String, String, String) ‣ public static

    void foo$default(String, String, String, int, Object) Default Argument: Java
  32. 60.

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

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

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

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

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

    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
  38. 66.

    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
  39. 67.

    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
  40. 68.

    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
  41. 69.

    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
  42. 70.

    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
  43. 71.

    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
  44. 72.

    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
  45. 73.

    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
  46. 74.

    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
  47. 75.

    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”
  48. 76.

    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”
  49. 77.