Java has anonymous functions, java8 after the addition of lambda support, but the development of basic use, so after switching to Kotlin was tortured to want to die, this special to learn about.

Anonymous functions

Introduction to the

Using callbacks prior to Java8 required defining an interface and instantiating the implementation of the interface with new when the function was called

Public interface OnItemClickListner {void onClick(int position, String name); } public void setOnItemClickListener(OnItemClickListner L) {mListener = l; } / / back to function mListView. SetOnItemClickListener (new setOnItemClickListener () {@ Override public void onCLick (int the position, String name) { //do something } });Copy the code

Using anonymous functions in Kotlin, you can pass functions that handle the logic directly

/ / after all, with similar mListView Java. SetOnClickListener (object: OnItemClickListner { override fun onCLick(int position, String name) {/ / do something}}) / / simplified mListView. SetOnItemClickListener {position, Name - > / / do something} / / if the position and name. Replace with less than you can use the underline mListView setOnItemClickListener {_, _ - > / / do something}Copy the code

So that’s an anonymous function… What is this call? Let’s start with the definition of an anonymous function

Lambda expressions and anonymous functions

Lambda expressions and anonymous functions are “functional literals,” that is, undeclared functions that are passed immediately as expressions.

max(strings, { a, b -> a.length < b.length })
Copy the code

Max takes a function as the second argument, which is equivalent to the named function below

fun compare(a:String, b:String) :Boolean {
    a.length < b.length
}
Copy the code

If functions can be arguments, how can they be used

Function as argument

The function variable takes an input parameter

We can instantiate a function and pass it in as an argument to the call function.

Ex. :

// define the anonymous function variable val performCalc: (Int, Int) -> String = {a, b -> val sum = a + b "$sum"} performCalc) fun printCalcResult(a: Int, b: Int, funcp: (Int, Int) -> String) { println(funcp(a, b)) }Copy the code

Function as an input parameter

If you don’t instantiate the function, can you call it directly

printCalcResult(3, 4, { a, b ->
        val sum = a + b
    	"$sum"
})
Copy the code

In Kotlin, there is room to change this call, and if the function argument is the last value, you can place the anonymous function after the parentheses

printCalcResult(3, 4) { a, b ->
        val sum = a + b
    	"$sum"
}
Copy the code

If you have only one argument, you can omit the parentheses

printCalcResult{ a, b ->
        val sum = a + b
    	"$sum"
}
Copy the code

Named function reference

We can pass the declared function as a variable directly to the function

// Calc fun calc(a: Int, b: Int): Int {return a + b} fun printCalc(a: Int, b: Int, p: (Int, Int) -> Int) {println(p(a, b))} printCalc(3, 4, ::calc)Copy the code

The return value is function

// A function of type () -> Int inline fun printCalc(): Return {val a = 3 val b = 4 a + b}} val p = printCalc() println(p())Copy the code