almost Greater order capabilities in Kotlin — Below the hood. | by Shangeeth Sivan | Jan, 2023 will lid the newest and most present instruction within the area of the world. get into slowly appropriately you comprehend skillfully and accurately. will addition your information cleverly and reliably

Let us take a look at a easy operate with a operate sort parameter in Kotlin. For simplicity, I’ve created this technique that accepts two parameters:

  • title of the occasion chain sort
  • completionListener of the Sort operate that returns Unit.
enjoyable insertEvent(eventName: String, completionListener: () -> Unit) 
eventRepository.insert(eventName) // Take into account this a delayed occasion

Let’s convert it to Kotlin bytecode and decompile it to the Java model.

Instruments -> Kotlin -> Present Bytecode -> Decompile (How one can Hyperlink)

public closing void insertEvent(@NotNull String eventName, @NotNull Function0 completionListener) 
Intrinsics.checkNotNullParameter(eventName, "eventName");
Intrinsics.checkNotNullParameter(completionListener, "completionListener");

Let’s break down every parameter:

  • title of the occasion – the info sort remains to be a string and is annotated with the @NonNull annotation in Java; this parameter appears easy.
  • completionListener – the info sort seems bizarre, it is known as Function0 ?? 🤔

What’s Function0? In case you do a management click on on Function0, you’ll navigate to Features.kt

Let’s examine what Features.kt comprises:

public interface Function0<out R> : Operate<R> 
public operator enjoyable invoke(): R

public interface Function1<in P1, out R> : Operate<R>
public operator enjoyable invoke(p1: P1): R

public interface Function2<in P1, in P2, out R> : Operate<R>
public operator enjoyable invoke(p1: P1, p2: P2): R


function0 it is nothing greater than an interface with technique name that has 0 parameters.

function1 is one other interface with name technique which has 1 parameter

function2 is one other interface with name technique which has 2 parameters
and proceed to Function22

Below that hood, our increased order operate completionListener has been outmoded by Function0 by Kotlin. If you already know a bit about generics, you must have the ability to work out what P1, P2, … and R are in Features.kt

P1, P2, … are the generic sorts for the parameters of completionListener
R is the generic return sort for the return sort of the operate in our case completionListener return sort

In case you nonetheless cannot work out what is going on on, attempt calling the identical operate you created in Kotlin from a Java file, you possibly can work out the way it works.

Since Kotlin is designed with Java interoperability in thoughts, present Java code will be known as from Kotlin naturally, and Kotlin code will also be used from Java with out difficulty.

//Calling the identical technique from Java world

public static void essential(String[] args)
// Calling the identical insertEvent from java world
eventHelper.insertEvent("check", new Function0<Unit>()
public Unit invoke()
return null;


As you possibly can see above, we’ve to create an nameless interior class for the Function0 interface to get the callback into the Java world.

Okay, let’s modify completionListener Y add a parameter:

// eliminated the eventName param
enjoyable insertEvent(completionListener: (check: String) -> Unit)

That is the generated code:

public closing void insertEvent(@NotNull Function1 completionListener) 
Intrinsics.checkNotNullParameter(completionListener, "completionListener");

function0 is now changed with function1 to assist the String parameter.

I hope this weblog has helped you perceive how increased order capabilities work below the hood.

Some further information:
Okay, will Kotlin assist any variety of parameters in our increased order capabilities? I acquired this query as a result of I observed one thing unusual in Features.kt, there are operate interfaces as much as Function22. What’s going to occur if I’ve greater than 22 parameters? Will Kotlin throw a compile error? We’ll see…

Let’s modify our completionListener have 23 parameters. 😅

// I've eliminated the string param and changed invoke technique with a println.
enjoyable insertEvent(
completionListener: (
test1: String, test2: String, test3: String, test4: String, test5: String, test6: String, test7: String, test8: String, test9: String, test10: String, test11: String, test12: String, test13: String, test14: String, test15: String, test16: String, test17: String, test18: String, test19: String, test20: String, test21: String, test22: String, test23: String,
) -> Unit
println("Simply Printing dont wish to invoke it :sweat:")

Let’s decompile…

public closing void insertEvent(@NotNull FunctionN completionListener) 
Intrinsics.checkNotNullParameter(completionListener, "completionListener");
String var2 = "Simply Printing dont wish to invoke it :sweat:";

Wow, now there’s one thing known as FunctionN. I will allow you to discover what that’s.

I hope the article roughly Greater order capabilities in Kotlin — Below the hood. | by Shangeeth Sivan | Jan, 2023 provides perception to you and is helpful for adjunct to your information

Higher order functions in Kotlin — Under the hood. | by Shangeeth Sivan | Jan, 2023

By admin