Kotlin Lambda expressions and how they simplify Android Development (KAD 07), kotlinlambda

Source: Internet
Author: User

Kotlin Lambda expressions and how they simplify Android Development (KAD 07), kotlinlambda

By Antonio Leiva

Time: Jan 5 and 2017

Link: https://antonioleiva.com/lambdas-kotlin/

 

 

Lambda expressions allow simpler mode functions, so they are one of the strongest tools for Kotlin and any other modern development languages.

 

In Java 6, we can only do this using the following method: declare an interface using a single method and implement those interfaces using an anonymous object.

 

Lambda expressions, especially those defined in Kotlin, enable the world of wireless possibilities. In the following content, we will understand these usage.

 

Lambda expression of Kotlin

 

Lambda expressions are the expression of functions. We have seen this example when interpreting setOnClickListener:

1 val view = findViewById(R.id.welcomeMessage)2 view.setOnClickListener { v -> navigateWithView(v) }

 

As you can see, define the input value of the function on the left (view in this example) and declare the operation to be implemented by the function on the right.

 

How to define functions that accept Lambda expressions

 

To define a function in Kotlin, follow these steps:

1 fun setOnClickListener(listener: (view: View) -> Unit){}

 

Because this function receives a function through the parameter or returns a function, this isHigh-order functions.

 

Kotlin and Java interop

 

The general method for calling this function may be as follows:

1 view.setOnClickListener({ v -> navigateWithView(v) })

 

This is a simple method we have seen, and we will be able to know it will help us do cooler things in the future.

 

 

This is because if the last parameter of a function is a function, we can extract it from the brackets:

1 view.setOnClickListener(){ v -> navigateWithView(v) }

 

In addition, if there is only one function as a parameter, we can save the following brackets:

1 view.setOnClickListener { v -> navigateWithView(v) }

 

DSL Creation

 

This allows us to create our own DSL, which can define micro-languages. The Kotlin reference website has an HTML example. Here we want to define a more brief method.

 

 

Suppose you want to create a code block to run on another thread. You can have a function that receives a function running in the background:

1 fun doAsync(f: () -> Unit) {2     Thread({ f() }).start()3 }

 

This function generates a thread that executes Runnable to run the function accepted as the independent variable. Runnable is a class with a single method in Java. it is replaced by a Lambda expression in Kotlin.

 

 

Now, in our code, the asynchronous code block is generated:

1 doAsync { 2     op1()3     op2()4     op3()5 }

 

In this way, everything in {} will be executed in the second thread.

 

Embedded Functions

 

What's annoying about functions received as independent variables is that the compiler needs to create classes for them, which affects performance. However, this can be easily solved with reserved words inline.

 

 

Because the Inline function replaces its code with its call during compilation, it has little impact on performance. Therefore, it does not need to be an object of the amount.

 

 

We can convert doAsync to an inline function:

1 inline fun doAsync(crossinline f: () -> Unit) {2     Thread({ f() }).start()3 }

 

In this example, because we call f () from another execution content (another Lambda expression), crossinline is required. Don't worry too much about this. The compiler will remind you when you need to use it.

 

Conclusion

 

As you can see, after using Lambda expressions, we can simplify a lot of our code, even things that cannot be implemented in Java.

 

 

In addition, the special naming rules of Kotlin allow us to create our own "development language" and create meaningful code blocks as needed.

 

 

Lambda expressions are very powerful. This book contains many situations where you can use them.

 

Kotlin and Java interop

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.