Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: What are named lambda arguments used for?

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?

like image 528
Jire Avatar asked Dec 26 '16 08:12

Jire


People also ask

What is the use of lambda expression in Kotlin?

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.

For what purpose lambda is used?

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.

What is the benefit of using lambda expression for event handling?

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.

When a lambda function has only one parameter what is its default name?

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.


Video Answer


1 Answers

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:

enter image description here

Then Tab or Enter, and you will get the lambda with the parameter names declared in the example definition:

enter image description here

Also, see this issue: Make use of named higher order function parameters

like image 59
hotkey Avatar answered Sep 28 '22 03:09

hotkey