Dissecting the inline keyword in Kotlin Suraj Shah

  • Slides: 73
Download presentation
Dissecting the inline keyword in Kotlin Suraj Shah, Software Engineer, Quiph

Dissecting the inline keyword in Kotlin Suraj Shah, Software Engineer, Quiph

About me: ● Contributor to Firefox Fenix, Mongo Stitch SDK, Realm etc. ● Kotlin,

About me: ● Contributor to Firefox Fenix, Mongo Stitch SDK, Realm etc. ● Kotlin, Java, Go. Lang ● Android, backend ● Photography, travelling and trekking Links: 1. Twitter: @shahsurajk 2. Github: /shahsurajk 3. Email: shahsurajk@gmail. com 4. Instagram: @a_random_traveller

QTalk: making phone calls fun & stress-free ➔ QTalk delivers synchronous communication with features

QTalk: making phone calls fun & stress-free ➔ QTalk delivers synchronous communication with features like shared web browsing, games, doodle without leaving the call screen ➔ Kotlin client and backend ➔ Default dialer on Android Links: 1. Twitter: @get. QTalk. App 2. Instagram: /QTalk. App 3. Facebook: /QTalk. App 4. Website: qtalk. io

Inline keyword 1. Why do we need it?

Inline keyword 1. Why do we need it?

Why do we need it? ➔ Definition: “Arranging things in a line” ➔ Why

Why do we need it? ➔ Definition: “Arranging things in a line” ➔ Why does Kotlin need to have a special keyword for this? ➔ Why can’t the compiler automatically do it? ➔ Role of Java versions here?

Java version compatibility? ➔ Lambdas? ➔ Lambdas in Java? ➔ Java 7 and “invokedynamic”

Java version compatibility? ➔ Lambdas? ➔ Lambdas in Java? ➔ Java 7 and “invokedynamic” bytecode instruction ➔ Compatibility and tools like retrolambda ➔ How does it affect Kotlin? ➔ Why should we care? ➔ Java 6 and Android?

Inline keyword 2. How does Kotlin achieve it?

Inline keyword 2. How does Kotlin achieve it?

How? 1. Lambdas and closures

How? 1. Lambdas and closures

1. Lambdas and closures ● Kotlin Lambdas: family of function types? ● Kotlin functions

1. Lambdas and closures ● Kotlin Lambdas: family of function types? ● Kotlin functions are first-class. ● Closures: a value captured by a lambda belonging to its outer scope

1. Lambdas and closures fun test. Lambdas(index: Int, my. Lambda: (index: Int) -> Unit){

1. Lambdas and closures fun test. Lambdas(index: Int, my. Lambda: (index: Int) -> Unit){ my. Lambda. invoke(index+1) } fun my. Big. Loop(){ value. In. Closure = "Why!" (0. . 50). for. Each { test. Lambdas(it) { lambda. Value-> println("$value. In. Closure $lambda. Value") } } }

1. Lambdas and closures fun test. Lambdas(index: Int, my. Lambda: (index: Int) -> Unit){

1. Lambdas and closures fun test. Lambdas(index: Int, my. Lambda: (index: Int) -> Unit){ my. Lambda. invoke(index+1) } fun my. Big. Loop(){ value. In. Closure = "Why!" (0. . 50). for. Each { test. Lambdas(it) { lambda. Value-> println("$value. In. Closure $lambda. Value") } } }

1. Lambdas and closures fun test. Lambdas(index: Int, my. Lambda: (index: Int) -> Unit){

1. Lambdas and closures fun test. Lambdas(index: Int, my. Lambda: (index: Int) -> Unit){ my. Lambda. invoke(index+1) } fun my. Big. Loop(){ value. In. Closure = "Why!" (0. . 50). for. Each { test. Lambdas(it) { lambda. Value-> println("$value. In. Closure $lambda. Value") } } }

How? 2. Call Sites

How? 2. Call Sites

fun i. Am. Groot() { println("I am Groot!") } class Who. Am. I {

fun i. Am. Groot() { println("I am Groot!") } class Who. Am. I { fun print. Me() { i. Am. Groot() // call site } }

fun i. Am. Groot() { println("I am Groot!") } class Who. Am. I {

fun i. Am. Groot() { println("I am Groot!") } class Who. Am. I { fun print. Me() { i. Am. Groot() // call site } }

How? 3. Non-inlined lambdas

How? 3. Non-inlined lambdas

fun test. Lambdas( index: Int, my. Lambda: (index: Int) -> Unit ){ my. Lambda.

fun test. Lambdas( index: Int, my. Lambda: (index: Int) -> Unit ){ my. Lambda. invoke(index+1) } fun my. Big. Loop(){ value. In. Closure = "Why!" (0. . 50). for. Each { test. Lambdas(it) { lambda. Value-> println("$value. In. Closure $lambda. Value") } } }

fun test. Lambdas( index: Int, my. Lambda: (index: Int) -> Unit ){ my. Lambda.

fun test. Lambdas( index: Int, my. Lambda: (index: Int) -> Unit ){ my. Lambda. invoke(index+1) } fun my. Big. Loop(){ value. In. Closure = "Why!" (0. . 50). for. Each { test. Lambdas(it) { lambda. Value-> println("$value. In. Closure $lambda. Value") } } }

fun test. Lambdas( index: Int, my. Lambda: (index: Int) -> Unit ){ my. Lambda.

fun test. Lambdas( index: Int, my. Lambda: (index: Int) -> Unit ){ my. Lambda. invoke(index+1) } fun my. Big. Loop(){ value. In. Closure = "Why!" (0. . 50). for. Each { test. Lambdas(it) { lambda. Value-> println("$value. In. Closure $lambda. Value") } } }

● Decompiled code for the lambda: final class Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1

● Decompiled code for the lambda: final class Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1 extends Lambda implements Function 1 { // $FF: synthetic field final String $value. In. Closure$inlined; Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1(String var 1) { super(1); this. $value. In. Closure$inlined = var 1; } // $FF: synthetic method // $FF: bridge method public Object invoke(Object var 1) { this. invoke(((Number)var 1). int. Value()); return Unit. INSTANCE; } public final void invoke(int lambda. Value) { String var 2 = this. $value. In. Closure$inlined + ' ' + lambda. Value; boolean var 3 = false; System. out. println(var 2); } }

● Decompiled code for the lambda: final class Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1

● Decompiled code for the lambda: final class Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1 extends Lambda implements Function 1 { // $FF: synthetic field final String $value. In. Closure$inlined; Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1(String var 1) { super(1); this. $value. In. Closure$inlined = var 1; } // $FF: synthetic method // $FF: bridge method public Object invoke(Object var 1) { this. invoke(((Number)var 1). int. Value()); return Unit. INSTANCE; } public final void invoke(int lambda. Value) { String var 2 = this. $value. In. Closure$inlined + ' ' + lambda. Value; boolean var 3 = false; System. out. println(var 2); } }

● Decompiled code for the lambda: final class Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1

● Decompiled code for the lambda: final class Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1 extends Lambda implements Function 1 { // $FF: synthetic field final String $value. In. Closure$inlined; Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1(String var 1) { super(1); this. $value. In. Closure$inlined = var 1; } // $FF: synthetic method // $FF: bridge method public Object invoke(Object var 1) { this. invoke(((Number)var 1). int. Value()); return Unit. INSTANCE; } public final void invoke(int lambda. Value) { String var 2 = this. $value. In. Closure$inlined + ' ' + lambda. Value; boolean var 3 = false; System. out. println(var 2); } }

● Decompiled code, call site: public static final void my. Big. Loop() { String

● Decompiled code, call site: public static final void my. Big. Loop() { String value. In. Closure = "Why!"; byte var 1 = 0; Iterable $this$for. Each$iv = (Iterable)(new Int. Range(var 1, 50)); int $i$f$for. Each = false; Iterator var 3 = $this$for. Each$iv. iterator(); while(var 3. has. Next()) { int element$iv = ((Int. Iterator)var 3). next. Int(); int var 6 = false; test. Lambdas( element$iv, (Function 1)(new Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1(value. In. Closure)) ); } }

● Decompiled code, call site: public static final void my. Big. Loop() { String

● Decompiled code, call site: public static final void my. Big. Loop() { String value. In. Closure = "Why!"; byte var 1 = 0; Iterable $this$for. Each$iv = (Iterable)(new Int. Range(var 1, 50)); int $i$f$for. Each = false; Iterator var 3 = $this$for. Each$iv. iterator(); while(var 3. has. Next()) { int element$iv = ((Int. Iterator)var 3). next. Int(); int var 6 = false; test. Lambdas( element$iv, (Function 1)(new Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1(value. In. Closure)) ); } }

● Decompiled code, call site: public static final void my. Big. Loop() { String

● Decompiled code, call site: public static final void my. Big. Loop() { String value. In. Closure = "Why!"; byte var 1 = 0; Iterable $this$for. Each$iv = (Iterable)(new Int. Range(var 1, 50)); int $i$f$for. Each = false; Iterator var 3 = $this$for. Each$iv. iterator(); while(var 3. has. Next()) { int element$iv = ((Int. Iterator)var 3). next. Int(); int var 6 = false; test. Lambdas( element$iv, (Function 1)(new Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1(value. In. Closure)) ); } }

● Decompiled code, call site: public static final void my. Big. Loop() { String

● Decompiled code, call site: public static final void my. Big. Loop() { String value. In. Closure = "Why!"; byte var 1 = 0; Iterable $this$for. Each$iv = (Iterable)(new Int. Range(var 1, 50)); int $i$f$for. Each = false; Iterator var 3 = $this$for. Each$iv. iterator(); while(var 3. has. Next()) { int element$iv = ((Int. Iterator)var 3). next. Int(); int var 6 = false; test. Lambdas( element$iv, (Function 1)(new Inline. Functions. Kt$my. Big. Loop$$inlined$for. Each$lambda$1(value. In. Closure)) ); } }

Non-inlined lambdas without closure? ● For non-inline lambdas without closure, a singleton is created

Non-inlined lambdas without closure? ● For non-inline lambdas without closure, a singleton is created rather than new object creation on every invocation

Non-inlined lambdas without closure? fun test. Lambdas(index: Int, my. Lambda: (index: Int) -> Unit){

Non-inlined lambdas without closure? fun test. Lambdas(index: Int, my. Lambda: (index: Int) -> Unit){ my. Lambda. invoke(index+1) } fun my. Big. Loop(){ (0. . 50). for. Each { test. Lambdas(it) { lambda. Value-> println("$lambda. Value") } } }

Non-inlined lambdas without closure? fun test. Lambdas(index: Int, my. Lambda: (index: Int) -> Unit){

Non-inlined lambdas without closure? fun test. Lambdas(index: Int, my. Lambda: (index: Int) -> Unit){ my. Lambda. invoke(index+1) } fun my. Big. Loop(){ (0. . 50). for. Each { test. Lambdas(it) { lambda. Value-> println("$lambda. Value") } } }

Non-inlined lambdas without closure? ● Decompiled code, call site: public static final void my.

Non-inlined lambdas without closure? ● Decompiled code, call site: public static final void my. Big. Loop() { byte var 0 = 0; Iterable $this$for. Each$iv = (Iterable)(new Int. Range(var 0, 50)); int $i$f$for. Each = false; Iterator var 2 = $this$for. Each$iv. iterator(); while(var 2. has. Next()) { int element$iv = ((Int. Iterator)var 2). next. Int(); int var 5 = false; test. Lambdas(element$iv, (Function 1)Inline. Functions. Kt$my. Big. Loop$1$1. INSTANCE); } }

Non-inlined lambdas without closure? ● Decompiled code, call site: public static final void my.

Non-inlined lambdas without closure? ● Decompiled code, call site: public static final void my. Big. Loop() { byte var 0 = 0; Iterable $this$for. Each$iv = (Iterable)(new Int. Range(var 0, 50)); int $i$f$for. Each = false; Iterator var 2 = $this$for. Each$iv. iterator(); while(var 2. has. Next()) { int element$iv = ((Int. Iterator)var 2). next. Int(); int var 5 = false; test. Lambdas(element$iv, (Function 1)Inline. Functions. Kt$my. Big. Loop$1$1. INSTANCE); } }

How? 4. Inlined lambdas

How? 4. Inlined lambdas

fun test. Lambdas( index: Int, my. Lambda: (index: Int) -> Unit ){ my. Lambda.

fun test. Lambdas( index: Int, my. Lambda: (index: Int) -> Unit ){ my. Lambda. invoke(index+1) } fun my. Big. Loop(){ value. In. Closure = "Why!" (0. . 50). for. Each { test. Lambdas(it) { lambda. Value-> println("$value. In. Closure $lambda. Value") } } }

inline fun test. Lambdas( index: Int, my. Lambda: (index: Int) -> Unit ){ my.

inline fun test. Lambdas( index: Int, my. Lambda: (index: Int) -> Unit ){ my. Lambda. invoke(index+1) } fun my. Big. Loop(){ value. In. Closure = "Why!" (0. . 50). for. Each { test. Lambdas(it) { lambda. Value-> println("$value. In. Closure $lambda. Value") } } }

● Decompiled code, not only of the lambda, but both: public static final void

● Decompiled code, not only of the lambda, but both: public static final void test. Lambdas(int index, @Not. Null Function 1 my. Lambda) { int $i$f$test. Lambdas = 0; Intrinsics. check. Parameter. Is. Not. Null(my. Lambda, "my. Lambda"); my. Lambda. invoke(index + 1); } public static final void my. Big. Loop() { String value. In. Closure = "Why!"; byte var 1 = 0; Iterable $this$for. Each$iv = (Iterable)(new Int. Range(var 1, 50)); int $i$f$for. Each = false; Iterator var 3 = $this$for. Each$iv. iterator(); while(var 3. has. Next()) { int element$iv = ((Int. Iterator)var 3). next. Int(); int var 6 = false; int $i$f$test. Lambdas = false; int lambda. Value = element$iv + 1; int var 9 = false; String var 10 = value. In. Closure + ' ' + lambda. Value; boolean var 11 = false; System. out. println(var 10); } }

● Decompiled code, not only of the lambda, but both: public static final void

● Decompiled code, not only of the lambda, but both: public static final void test. Lambdas(int index, @Not. Null Function 1 my. Lambda) { int $i$f$test. Lambdas = 0; Intrinsics. check. Parameter. Is. Not. Null(my. Lambda, "my. Lambda"); my. Lambda. invoke(index + 1); } public static final void my. Big. Loop() { String value. In. Closure = "Why!"; byte var 1 = 0; Iterable $this$for. Each$iv = (Iterable)(new Int. Range(var 1, 50)); int $i$f$for. Each = false; Iterator var 3 = $this$for. Each$iv. iterator(); while(var 3. has. Next()) { int element$iv = ((Int. Iterator)var 3). next. Int(); int var 6 = false; int $i$f$test. Lambdas = false; int lambda. Value = element$iv + 1; int var 9 = false; String var 10 = value. In. Closure + ' ' + lambda. Value; boolean var 11 = false; System. out. println(var 10); } }

● Decompiled code, not only of the lambda, but both: public static final void

● Decompiled code, not only of the lambda, but both: public static final void test. Lambdas(int index, @Not. Null Function 1 my. Lambda) { int $i$f$test. Lambdas = 0; Intrinsics. check. Parameter. Is. Not. Null(my. Lambda, "my. Lambda"); my. Lambda. invoke(index + 1); } public static final void my. Big. Loop() { String value. In. Closure = "Why!"; byte var 1 = 0; Iterable $this$for. Each$iv = (Iterable)(new Int. Range(var 1, 50)); int $i$f$for. Each = false; Iterator var 3 = $this$for. Each$iv. iterator(); while(var 3. has. Next()) { int element$iv = ((Int. Iterator)var 3). next. Int(); int var 6 = false; int $i$f$test. Lambdas = false; int lambda. Value = element$iv + 1; int var 9 = false; String var 10 = value. In. Closure + ' ' + lambda. Value; boolean var 11 = false; System. out. println(var 10); } }

● Decompiled code, not only of the lambda, but both: public static final void

● Decompiled code, not only of the lambda, but both: public static final void test. Lambdas(int index, @Not. Null Function 1 my. Lambda) { int $i$f$test. Lambdas = 0; Intrinsics. check. Parameter. Is. Not. Null(my. Lambda, "my. Lambda"); my. Lambda. invoke(index + 1); } public static final void my. Big. Loop() { String value. In. Closure = "Why!"; byte var 1 = 0; Iterable $this$for. Each$iv = (Iterable)(new Int. Range(var 1, 50)); int $i$f$for. Each = false; Iterator var 3 = $this$for. Each$iv. iterator(); while(var 3. has. Next()) { int element$iv = ((Int. Iterator)var 3). next. Int(); int var 6 = false; int $i$f$test. Lambdas = false; int lambda. Value = element$iv + 1; int var 9 = false; String var 10 = value. In. Closure + ' ' + lambda. Value; boolean var 11 = false; System. out. println(var 10); } }

Magical isn’t it?

Magical isn’t it?

Real magic in action: ● Type reification

Real magic in action: ● Type reification

Reification: what? “The literal meaning of the word ‘reified’ is to convert into or

Reification: what? “The literal meaning of the word ‘reified’ is to convert into or regard as a concrete thing”

Reification: context ● Generics

Reification: context ● Generics

Reification: context: generics: ● Java, type erasure: public static void main(String[] args) { final

Reification: context: generics: ● Java, type erasure: public static void main(String[] args) { final List<String> s = new Array. List<>(); System. out. println(s instanceof List<Object>); }

Reification: context: generics: ● Java, type erasure: public static void main(String[] args) { final

Reification: context: generics: ● Java, type erasure: public static void main(String[] args) { final List<String> s = new Array. List<>(); System. out. println(s instanceof List<Object>); } ● Compiler error: “Illegal generic type for instance of”

Reification: context: generics: ● Java, type erasure: public static void main(String[] args) { final

Reification: context: generics: ● Java, type erasure: public static void main(String[] args) { final List<String> s = new Array. List<>(); System. out. println(s instanceof List); } ● Works! ����

Reification: how? ● Can inline help?

Reification: how? ● Can inline help?

Reification: how? : inline try? ● Kotlin generic type check without inline and reified:

Reification: how? : inline try? ● Kotlin generic type check without inline and reified: fun <T> reified. Test(){ println(T: : class. simple. Name == String: : class. simple. Name) } reified. Test<String>()

Reification: how? : inline try? ● Kotlin generic type check without inline and reified:

Reification: how? : inline try? ● Kotlin generic type check without inline and reified: fun <T> reified. Test(){ println(T: : class. simple. Name == String: : class. simple. Name) } reified. Test<String>() ● Compiler error: “Cannot use 'T' as reified type parameter. Use a class instead. ”

Reification: how? : inline try? ● Kotlin generic type check with inline and reified:

Reification: how? : inline try? ● Kotlin generic type check with inline and reified: inline fun <reified T> reified. Test(){ println(T: : class. simple. Name == String: : class. simple. Name) } reified. Test<String>() ● Voila! It works!

Reification: how? : internals ● Test Code: inline fun <reified T> reified. Test(){ println(T:

Reification: how? : internals ● Test Code: inline fun <reified T> reified. Test(){ println(T: : class. simple. Name == String: : class. simple. Name) } fun test(){ reified. Test<String>() }

Reification: how? : internals ● Decompiled Code (prettified): public static final void test() {

Reification: how? : internals ● Decompiled Code (prettified): public static final void test() { int $i$f$reified. Test = false; boolean var 1 = Intrinsics. are. Equal( Reflection. get. Or. Create. Kotlin. Class(String. class). get. Simple. Name(), Reflection. get. Or. Create. Kotlin. Class(String. class). get. Simple. Name() ); boolean var 2 = false; System. out. println(var 1); } ● Compiler copy-pasta!

Reification: how? : internals ● Decompiled Code original logic block (prettified): public static final

Reification: how? : internals ● Decompiled Code original logic block (prettified): public static final void reified. Test() { int $i$f$reified. Test = 0; Intrinsics. reified. Operation. Marker(4, "T"); boolean var 1 = Intrinsics. are. Equal( Reflection. get. Or. Create. Kotlin. Class(Object. class). get. Simple. Name(), Reflection. get. Or. Create. Kotlin. Class(String. class). get. Simple. Name() ); boolean var 2 = false; System. out. println(var 1); }

Reification: how? : internals ● Decompiled Code original logic block (prettified): public static final

Reification: how? : internals ● Decompiled Code original logic block (prettified): public static final void reified. Test() { int $i$f$reified. Test = 0; Intrinsics. reified. Operation. Marker(4, "T"); boolean var 1 = Intrinsics. are. Equal( Reflection. get. Or. Create. Kotlin. Class(Object. class). get. Simple. Name(), Reflection. get. Or. Create. Kotlin. Class(String. class). get. Simple. Name() ); boolean var 2 = false; System. out. println(var 1); } ● Original logic block remains the same!

Beyond the horizon: ● Inline classes

Beyond the horizon: ● Inline classes

Inline classes: ● ● annotation in Android? Have compile time restrictions on objects Example:

Inline classes: ● ● annotation in Android? Have compile time restrictions on objects Example: inline class Fosdem. Person(val can. Have. ASeat: Boolean) @Int. Def object Fosdem. Rooms { val SPEAKER = Fosdem. Person(true) val ATTENDEE = Fosdem. Person(false) } Used to restrict types.

Inline classes: ● Advantages: ○ Opens up possibilities to have compile-time-only data structures like

Inline classes: ● Advantages: ○ Opens up possibilities to have compile-time-only data structures like UInt, ULong, UShort, UByte ○ They make amazing database IDs, post by Jake Wharton ○ Reduces the need to create wrappers when using primitives!

Inline classes: ● Disadvantages: ○ Mangling: appends a hash code to the end of

Inline classes: ● Disadvantages: ○ Mangling: appends a hash code to the end of the function name inline class UInt(val x: Int) // Represented as 'public final void compute(int x)' on the JVM fun compute(x: Int) { } // Also represented as 'public final void compute(int x)' on the JVM! fun compute(x: UInt) { } ○ Still has an experimental status ○ Sounds similar to a Type. Alias

Advance concepts 1. Crossinline

Advance concepts 1. Crossinline

1. Crossinline ● Concept: If we do not wish non-local returns to happen, which

1. Crossinline ● Concept: If we do not wish non-local returns to happen, which is controlling whether the lambda can return the function “outside its scope” or simply put the parent function, then we can specify it with a crossinline modifier. ● Too much to grab? Let’s see some code

1. Crossinline fun mixed. Lambda. Holder( crossinline non. Local. Return. Blocked. Lambda: () ->

1. Crossinline fun mixed. Lambda. Holder( crossinline non. Local. Return. Blocked. Lambda: () -> Unit, normal. Lambda: () -> Unit ){ non. Local. Return. Blocked. Lambda. invoke() normal. Lambda. invoke() }

1. Crossinline fun mixed. Lambda. Holder( crossinline non. Local. Return. Blocked. Lambda: () ->

1. Crossinline fun mixed. Lambda. Holder( crossinline non. Local. Return. Blocked. Lambda: () -> Unit, normal. Lambda: () -> Unit ){ non. Local. Return. Blocked. Lambda. invoke() normal. Lambda. invoke() }

1. Crossinline ● Kotlin code, calling function: fun main(){ mixed. Lambda. Holder(non. Local. Return.

1. Crossinline ● Kotlin code, calling function: fun main(){ mixed. Lambda. Holder(non. Local. Return. Blocked. Lambda = { // this only returns the lambda and not the main function return@mixed. Lambda. Holder // this is permitted // this throws a compiling error, saying 'return' is not allowed here. return }, normal. Lambda = { return@mixed. Lambda. Holder // this can return both the lambda // as well as the main function return }) }

1. Crossinline ● Kotlin code, calling function: fun main(){ mixed. Lambda. Holder(non. Local. Return.

1. Crossinline ● Kotlin code, calling function: fun main(){ mixed. Lambda. Holder(non. Local. Return. Blocked. Lambda = { // this only returns the lambda and not the main function return@mixed. Lambda. Holder // this is permitted } // this throws a compiling error, saying 'return' is not allowed here. return }, normal. Lambda = { return@mixed. Lambda. Holder // this can return both the lambda // as well as the main function return })

1. Crossinline ● Kotlin code, calling function: fun main(){ mixed. Lambda. Holder(non. Local. Return.

1. Crossinline ● Kotlin code, calling function: fun main(){ mixed. Lambda. Holder(non. Local. Return. Blocked. Lambda = { // this only returns the lambda and not the main function return@mixed. Lambda. Holder // this is permitted // this throws a compiling error, saying 'return' is not allowed here. return }, normal. Lambda = { return@mixed. Lambda. Holder // this can return both the lambda // as well as the main function return }) }

1. Crossinline ● Kotlin code, calling function: fun main(){ mixed. Lambda. Holder(non. Local. Return.

1. Crossinline ● Kotlin code, calling function: fun main(){ mixed. Lambda. Holder(non. Local. Return. Blocked. Lambda = { // this only returns the lambda and not the main function return@mixed. Lambda. Holder // this is permitted } // this throws a compiling error, saying 'return' is not allowed here. return }, normal. Lambda = { return@mixed. Lambda. Holder // this can return both the lambda // as well as the main function return })

Advance concepts 2. Noinline

Advance concepts 2. Noinline

2. Noinline Concept: Noinline, as the name goes, doesn’t inline a lambda and keeps

2. Noinline Concept: Noinline, as the name goes, doesn’t inline a lambda and keeps it as is. But why do we need this then? Well, consider a case where you have a lambda, but the lambda is further being consumed in a function which is not inlined, this could be a system API etc. Thus there’s no way for the compiler to inline it. Forcing us to mark it with a noinline modifier, at which the compiler just skips inlining it. What’s better than seeing the code? Nothing.

● Kotlin code: inline fun parameter. Passed. To. Other. Inline. Function( lambda 1: ()

● Kotlin code: inline fun parameter. Passed. To. Other. Inline. Function( lambda 1: () -> Unit, noinline lambda 2: () -> Boolean ){ // normal invoke, this is a normal lambda 1. invoke() // passing the lambda to another function which doesn't inline this lambda // will throw an error if lambda 2 is not marked as noinline some. Non. Inlined. Lambda. Consuming. Function(lambda 2) } fun some. Non. Inlined. Lambda. Consuming. Function(lambda: () -> Boolean): Boolean { return lambda. invoke() }

● Kotlin code: inline fun parameter. Passed. To. Other. Inline. Function( lambda 1: ()

● Kotlin code: inline fun parameter. Passed. To. Other. Inline. Function( lambda 1: () -> Unit, noinline lambda 2: () -> Boolean ){ // normal invoke, this is a normal lambda 1. invoke() // passing the lambda to another function which doesn't inline this lambda // will throw an error if lambda 2 is not marked as noinline some. Non. Inlined. Lambda. Consuming. Function(lambda 2) } fun some. Non. Inlined. Lambda. Consuming. Function(lambda: () -> Boolean): Boolean { return lambda. invoke() }

● Kotlin code: inline fun parameter. Passed. To. Other. Inline. Function( lambda 1: ()

● Kotlin code: inline fun parameter. Passed. To. Other. Inline. Function( lambda 1: () -> Unit, noinline lambda 2: () -> Boolean ){ // normal invoke, this is a normal lambda 1. invoke() // passing the lambda to another function which doesn't inline this lambda // will throw an error if lambda 2 is not marked as noinline some. Non. Inlined. Lambda. Consuming. Function(lambda 2) } // not inlined! fun some. Non. Inlined. Lambda. Consuming. Function(lambda: () -> Boolean): Boolean { return lambda. invoke() }

● Kotlin code: inline fun parameter. Passed. To. Other. Inline. Function( lambda 1: ()

● Kotlin code: inline fun parameter. Passed. To. Other. Inline. Function( lambda 1: () -> Unit, noinline lambda 2: () -> Boolean ){ // normal invoke, this is a normal lambda 1. invoke() // passing the lambda to another function which doesn't inline this lambda // will throw an error if lambda 2 is not marked as noinline some. Non. Inlined. Lambda. Consuming. Function(lambda 2) } fun some. Non. Inlined. Lambda. Consuming. Function(lambda: () -> Boolean): Boolean { return lambda. invoke() }

That’s all folks!

That’s all folks!

Links: 1. Blog (Draft) 2. Good read on inline functions in general 3. Invokedynamic

Links: 1. Blog (Draft) 2. Good read on inline functions in general 3. Invokedynamic in java: link 4. Inline classes as great database ids: link 5. QTalk App: bit. ly/QTalk. App 6. Link to this presentation: bit. ly/power-of-inline