Slide 1

Slide 1 text

How Kotlin implements features Java doesn’t have @lvla0805

Slide 2

Slide 2 text

Ѫᖒ๖ (Moyuru Aizawa) lvla lvla0805 - Kotlin engineer at CyberAgent, Inc. - FRESH!

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Bytecode

Slide 6

Slide 6 text

NonNull / Nullable Extension Function Named Argument / Default Argument

Slide 7

Slide 7 text

NonNull / Nullable

Slide 8

Slide 8 text

fun nonNullInt(number: Int) { number.toString() } NonNull: Kotlin

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

public final void nonNullInt(int number) { String.valueOf(number); } NonNull: Java

Slide 12

Slide 12 text

fun nullableInt(number: Int?) { number?.toString() } Nullable: Kotlin

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

public final void nullableInt(@Nullable Integer number) { if(number != null) { String.valueOf(number.intValue()); } } Nullable: Java

Slide 17

Slide 17 text

fun nonNullString(string: String) { string.length } NonNull: Kotlin

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

public final void nonNullString(@NotNull String string) { Intrinsics.checkParameterIsNotNull(string, "string"); string.length(); } NonNull: Java

Slide 22

Slide 22 text

private fun nonNullString(string: String) { string.length } NonNull: Kotlin

Slide 23

Slide 23 text

private final nonNullString(Ljava/lang/String;)V L0 LINENUMBER 7 L0 ALOAD 1 INVOKEVIRTUAL java/lang/String.length ()I POP NonNull: Bytecode

Slide 24

Slide 24 text

private fun nullableString(string: String?) { string?.length } Nullable: Kotlin

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

private final void nullableString(String string) { if(string != null) { string.length(); } } Nullable: Java

Slide 27

Slide 27 text

Conclusion of NonNull/Nullable Primitive NonNull -> Primitive Primitive Nullable -> BoxType ClassType NonNull -> ClassType, Check Null ClassType Nullable -> ClassType Safe Call -> IFNULL

Slide 28

Slide 28 text

Extension Function

Slide 29

Slide 29 text

fun String.println() { println(this) } "Hello World".println() // out: Hello World Extension Function

Slide 30

Slide 30 text

Package Level Extension Function

Slide 31

Slide 31 text

// in StringExtension.kt fun String.println() { println(this) } Package Level Extension Function: Kotlin

Slide 32

Slide 32 text

public final class StringExtensionKt { public final static println(Ljava/lang/String;)V … } Package Level Extension Function: Bytecode

Slide 33

Slide 33 text

public final class StringExtensionKt { public final static println(Ljava/lang/String;)V … } Package Level Extension Function: Bytecode

Slide 34

Slide 34 text

public final class StringExtensionKt { public final static println(Ljava/lang/String;)V … } Package Level Extension Function: Bytecode

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Class Level Extension Function

Slide 37

Slide 37 text

class Foo { fun String.println() { println(this) } } Class Level Extension Function: Kotlin

Slide 38

Slide 38 text

public final class Foo { public final println(Ljava/lang/String;)V … } Class Level Extension Function: Bytecode

Slide 39

Slide 39 text

Extension Function Package Level Extension Function -> Static Method Class Level Extension Function -> Method

Slide 40

Slide 40 text

Package Level Extension Function Package Level Extension Function -> Static Method

Slide 41

Slide 41 text

Package Level Extension Function Public Package Level Extension Function -> Public Static Method Private Package Level Extension Function -> Private Static Method

Slide 42

Slide 42 text

Scope of Package Level Extension Public Package Level Extension Function -> Anywhere Private Package Level Extension Function -> Same file

Slide 43

Slide 43 text

Private Package Level Extension Function Private Package Level Extension Function -> Private Static Method

Slide 44

Slide 44 text

// in Bar.kt private fun Foo.doSomething() { } class Foo { init { doSomething() } } Private Package Level Extension Function: Kotlin

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

Named Argument Default Argument

Slide 50

Slide 50 text

fun foo(one: String, two: String, three: String) { } foo(three = "tres", one = "uno", two = "dos") Named Argument: Kotlin

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

String var1 = "dos"; String var2 = "uno"; String var3 = "tres"; this.foo(var2, var1, var3); Named Argument: Java

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

‣ public static void foo(String, String, String) ‣ public static void foo$default(String, String, String, int, Object) Default Argument: Java

Slide 59

Slide 59 text

foo(two = "dos") Default Argument: Kotlin

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

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”

Slide 76

Slide 76 text

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”

Slide 77

Slide 77 text

Thank you