In Kotlin, it is possible to give lambda arguments names in their definition.
fun example(lambda: (a: Int, b: Int) -> Int)
As you can see, a
and b
are named in the lambda. I thought this might be really useful information for an IDE, to generate lambdas with the parameter names filled-in.. but at least with IntelliJ the functionality either doesn't exist or works in a way I'm unaware.
So, what are the uses of named lambda arguments? Do they change the compile output in some way? And are there any tricks you can use them for?
A lambda expression is a shorter way of describing a function. It doesn't need a name or a return statement. You can store lambda expressions in a variable and execute them as regular functions. They can also be passed as parameters to other functions or be the return value.
A lambda function is typically used to define the filtering logic and is passed as the first argument of filter() . An iterable like a list object is passed as the second argument to the filter function.
Fewer Lines of Code − One of the most benefits of a lambda expression is to reduce the amount of code. We know that lambda expressions can be used only with a functional interface.
Lambda expressions Like anonymous functions, lambda expressions allow no default parameters and cannot be called with named arguments. Since they are stored immediately as a function type like (Int, Int) -> Int , they undergo the same restrictions as function types referring to actual functions.
Currently, no language feature uses these names, and, as far as I know, the only thing that does is the generation of lambda templates by the IDE plugin:
Then Tab or Enter, and you will get the lambda with the parameter names declared in the example
definition:
Also, see this issue: Make use of named higher order function parameters
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With