What is the purpose of reading this article?

Kotlin lambda and higher-order functions are still a little confusing, so I’m going to write a new one. After learning this one, I believe you really understand the nature of lambda and higher-order functions in Kotlin, and you are no longer afraid to use them.

Can functions be passed as arguments in Java?

It is not allowed to pass functions as arguments in Java. Most Java developers know this. But Java opens a similar hole. That’s the callback function. The purpose of a callback function is to pass a function as an argument to another function.

Code similar to the following:

We’re actually passing in what looks like an object, but we’re using methods in that object.

So the idea is to actually pass one function into another function.

Although Syntactically Java does not support passing functions as arguments, in practice Java gives us a similar backdoor. That’s the callback function. But we need to know that his backdoor is also built on the basis of actually passing objects in.

Can functions in Kotlin be passed as arguments?

The answer is yes. The JVM does not support passing functions as arguments, so Kotlin does not support that either. But Kotlin can use syntactic sugar, Let kotlin developers think that functions can be passed as arguments in Kotlin.

Let’s take an example:

After looking at the above examples, we can conclude that kotlin’s function is not a type, but we can abstract a way to define a function based on its arguments and return values. That’s my red comment in the picture.

Now that we have this foundation, we can pass in the function as an argument in Kotlin.

Since function A is defined as a higher-order function that can be called as an argument, let’s see how it is used.

At this point we should conclude that the function in Kotlin is essentially passing an object when passed as an argument.

Since passing an object can also support passing anonymous objects, right? It’s also possible:

Lambda in Kotlin in relation to anonymous functions

It’s an equal relationship, and I’ll write a couple more examples just to get the idea.

This is a pretty familiar function that adds a click event to a view.

Essentially we can see that the setOnclickListener function wants to get a void function that takes view.

So we can write this:

Is it all right? Let’s give him an anonymous object of type function and the argument is of type view and the return value is Unit and this is written if you can make sense of what’s up there and you can see there’s no pressure.

Then we can use lambda to simplify our writing:

V: View represents the parameter type of the anonymous function, and the last line in the lambda represents the return value.

Since our argument type is a function type that has no return value, the println function has no return value.

Or even more shorthand:

You can see that the argument declaration is also omitted.

Lambda is part of a declaration statement

Finally, let’s write a code that reinforces kotlin’s higher-order function relationship to lambda.

First of all,

We declare an object of type function. He is anonymous. It occurred to us that anonymous functions could not be simplified by lambda?

And the last line of the lambda is always the return value of the anonymous function

So we wanted to abbreviate it like this:

You see there is an error, why is that? Well, it’s pretty simple, because there’s no way for the compiler to know what type of argument your lambda or anonymous function is, right

How can you use an argument without declaring its type? Isn’t that a definite error?

So if we specify the type of the function (argument and return value) in the declaration, we can use lambda shorthand.