Kotlin: What are named lambda arguments used for?

985 views Asked by At

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?

1

There are 1 answers

0
hotkey On BEST ANSWER

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