Antonio Leiva
Time:Jan 5,
Original link:https://antonioleiva.com/lambdas-kotlin/
Because lambda expressions allow simpler ways to model functions, it is one of the strongest tools for Kotlin and any other modern development language.
In Java6, we can only do this by declaring interfaces in a single way and implementing those interfaces with anonymous objects.
Lambda expressions, especially in the way that can be defined in Kotlin, open the world of wireless possibilities for us. In the next section, we'll look at these usages.
Lambda expression for Kotlin
The lambda expression is the way the function is represented, and we have seen this example when explaining Setonclicklistener:
1 val view = Findviewbyid (r.id.welcomemessage)2 view.setonclicklistener {V Navigatewithview (v)}
As you can see, the left defines the input value of the function (in this case, the view), and the right side declares the operation to be implemented by the function.
How to define a function that accepts a lambda expression
If we were to define the functions ourselves in Kotlin, we would need to do the following:
1 Fun Setonclicklistener (listener: (View:view), Unit) {}
Since this function receives a function through a parameter, or returns a function, this is a higher-order function .
Kotlin and Java Interop
The general way to call this function might be as follows:
1 view.setonclicklistener ({v-navigatewithview (v)})
This is a relatively simple way we've seen it, and we'll know it's going to help us do something cooler in the future.
This is because if the last parameter of the function is a function, we can extract it from the parentheses:
1 view.setonclicklistener () {v-navigatewithview (v)}
In addition, if there is only one function as a parameter, we can omit the parentheses:
1 View.setonclicklistener {v-navigatewithview (v)}
DSL creation
This allows us to create our own DSL , which defines the micro language. There are HTML examples in the Kotlin reference site , and here we define a more introductory approach.
Suppose you want to create a block of code that runs on another thread. You can have a function that receives a function that runs in the background:
1 Fun Doasync (f: (), Unit) {2 Thread ({f ()}). Start ()3 }
This function produces a thread that executes a Runnable run the function that is accepted as an argument. Runnable is a class with a single method in Java, in Kotlin by the Lambda expression substitution.
Now, in our code, we generate an asynchronous block of code:
12 op1 ()3 op2 ()4 op3 () 5 }
this in Everything inside {} will be executed in the second thread.
Inline Functions
functions that are received as arguments are annoying that the compiler needs to create classes for them, which will affect performance. However, this can be easily solved with the reserved word inline .
because Inline The function is to replace its code at compile time with a call to it, so there is less performance impact. To do this, it does not require an amount of an object.
we can convert Doasync to a inline function:
1 Inline fun Doasync (crossinline F: (), Unit) {2 Thread ({f ()}). Start () 3 }
in this example, as we execute the content from another (another Lambda expression) called in an f () , so ask Crossinline . Don't worry too much about this, the compiler will remind you when you need to use it.
Conclusion
as you can see, use Lambda expression, we can simplify a lot of our code, even in Java can not be achieved in the.
in addition, Kotlin Special naming rules enable us to create our own "development language", and can also create meaningful blocks of code as needed.
Lambda expressions are very powerful, and this book contains a lot of situations where you can use them differently.
Kotlin and the Java Interop
Kotlin lambda expressions and how they simplify Android development (KAD 07)