Useful documentation for any lambda: (foo: Bar) -> Unit

Function declarations contain names for their parameters. That’s as ordinary as it gets in programming.

The definition of anonymous functions and lambdas contain named parameters, though it might be implicitly named and used with lambdas. Depending on the context of the function or lambda declaration, there might not even be any need to declare the parameter type.

When it comes to declaring the type of the function itself, the focus is on the type. Thus a common declaration is just (Int) -> Unit, a function expecting a single parameter of type Int. Knowing the context of this, it should be fairly easy to figure out the meaning of it later. But even with only one more Int parameter, the purpose of each would become arbitrary at once. But…

The function type notation can optionally include names for the function parameters: _(x: Int, y: Int) -> Point_. These names can be used for documenting the meaning of the parameters.

#kotlin #mobile-apps #programming #developer

Kotlin Function Type Declarations with Named Parameters
10.55 GEEK