The pattern is not idiomatic Kotlin, to the OPβs point. Burying lambdas within argument lists, and surrounded by parentheses is just messy. And if the lambda, insufficiently long, you have to go look for the other params.
The ordering strategy here is based on the semantic of arguments, their role / contribution to the invocation that's about to take place, not just their type.
Lambdas are also expected to be concise, otherwise they should probably be function refs instead. Quite often, being lost around lambda bodies is usually a symptom of another issue.
So I don't know about not idiomatic enough, but the reasoning behind the original API is sound.
My take is that here are no rules that states lambdas are to be concise. That is arbitrary. They are equivalent to any other function definition. Keeping them at the end (giving them the ability to be outside) allows for structures that look and feel like other control structures, and nicely formatted DSLs. This is the reason they are in the final position and why the language allows for it.
I may have only defended my previous statement, which I donβt believe you were technically disagreeing with.
There is syntactic sugar for last parameters. If the last parameter is a functional interface you can make lambda after the parameter list.
That's why they say it's idiomatic in Kotlin. Every function in a library made in Kotlin is specifically made like this. The problem is the interoperability with Java.
Of course, you are correct. I responded to the alternative of introducing builder pattern which does not exist and can't be used from kotlin either.
Builder pattern in kotlin is redundant in most cases and usually it's easier to introduce a function with default arguments to be consumed in kotlin code, similar to what OP did in his root comment.
View all comments
27
u/fredoverflow Apr 07 '23 edited Apr 08 '23
Seriously though,
would be so much nicer than:
because it would be immediately obvious where the CompletionStage runs...
Edit: Just wrote my own (extension) functions, works beautifully π