Kotlin: Function type, Function literal, Lambda expression, and Anonymous function
Function Type
We know data types. Similarly, we have function type too in kotlin.
Signature / Syntax
How to call/execute a function type?
Usage:
Function type as an interface
We can implement a function type like an interface in a kotlin class. When we implement a function type, we get a " invoke " method to override a signature similar to the implemented function type.
Example:
Function type as a parameter
As a function parameter in a higher-order function. The function that takes one or more function types as a parameter/s or/and the function that returns a function type is known as a higher-order function.
There is one thing to remember here:
A new function object will be created for each function type of a higher-order function.
How to call/use/execute such a higher-order function?
We know how to call a function, but we may not know how to pass a function type argument/s in any higher-order function!
We can pass function type arguments using function literal, lambda expression, or anonymous function. Let us check these all one by one.
Function literal
Signature / Syntax: 1
{ comma separated pascal parameters -> business logic }
Example:
Note that the last statement in a function literal is considered as a return statement. So, the last statement in a function literal makes the return type.
We can use function literal to pass as a function type argument for higher-order function as below:
OR if the function type parameter is the last in the higher-order function, we can write our function literal after the closing function parenthesis as below:
OR if the higher-order function has only one parameter or if the function type parameter is only parameter in a higher-order function like below:
While calling such a higher-order function, we can omit the function call parentheses like below:
Syntax / Signature 2
Also, if the function type is the only parameter in a higher-order function and if the function type also has only one parameter like below:
While passing such a function literal, in addition to function call parentheses, we can also omit the parameter and an arrow ->
! We just write core business logic between curly braces and we can access our single parameter through the keyword it like below:
And if the higher-order function has only one function type parameter and the function type has no parameter like below:
We can simply write our business logic between curly braces right after the higher-order function name as below:
Types:
There are two types of function literals.
- Lambda expression
- Anonymous function
Lambda expression
Lambda expression is another way to define a function concisely.
Signature / Syntax 1
We can either specify explicit function type and let the data type of parameters be inferred in the below format:
nameOfTheLambda: (explicit function type) = { comma separated parameter: Inferred data type -> business logic }
Or we can give explicit type annotation to parameters and let the function type be inferred like the below format:
nameOfTheLambda: (inferred function type) = { comma separated parameter: Explicit dataType -> business logic}
Or we can write both function type and data type of parameter (but why?) like below format:
nameOfTheLambda: (explicit function type) = { comma separated parameter: Explicit dataType -> business logic}
Let us see an example:
Signature / Syntax 2 for special case
If a lambda has only one parameter, we can omit everything except a business logic inside the curly braces, and we can access the single argument using the keyword: it.
Example:
Suppose we have a lambda expression like the below:
We can write the equivalent lambda as below:
Summary for a lambda expression having a single parameter
Usage: How to call / execute / use lambda expression
We can use the above lambda expression directly like below:
OR we can pass a lambda as a function type argument to a higher-order function like below:
Sometimes, the common term “lambda” represents a function type, function literal, or a lambda expression.
Anonymous function
Signature / Syntax
We can store an anonymous function in a variable like the below:
Usage: How to use an anonymous function
We use an anonymous function as an argument for a higher-order function like the below:
We can use an anonymous function through a variable to pass as an argument in a higher-order function as below:
That’s all! If you have read this article, if this article has helped you, you can click on that clap icon for few times 😉
Applauds and creative critics are always welcome 😇
We will learn about extension function and receiver type in the next part.
Thanks for reading the article! Have a great day 😇
You can follow me here on medium or various social networks to get the notification when I publish a new article or to discuss android development, soft skill development, or various ideas and hacks. Let us become better together.
Let us learn together from each other’s experiences.
Let us be Connected
https://www.linkedin.com/in/srdpatel
https://twitter.com/LogicalSagar
Tags: kotlin, function type, function literal, lambda expression, an anonymous function, higher-order function
Thanks to nwillc for reminding me about the implementation of a function type as an interface 😇.