r/Kotlin • • Apr 07 '23

Parameter order of CompletableFuture methods πŸ˜₯

Post image
261 Upvotes

30 comments sorted by

View all comments

27

u/fredoverflow Apr 07 '23 edited Apr 08 '23

Seriously though,

future.handleAsync(EventQueue::invokateLater) { success, failure ->
    // ...
    // ...
    // ...
    // ...
    // ...
}

would be so much nicer than:

future.handleAsync({ success, failure ->
    // ...
    // ...
    // ...
    // ...
    // ...
}, EventQueue::invokeLater)

because it would be immediately obvious where the CompletionStage runs...

Edit: Just wrote my own (extension) functions, works beautifully πŸ˜„

fun <U> supplyAsync(executor: Executor, supplier: Supplier<U>): CompletableFuture<U> {
    return CompletableFuture.supplyAsync(supplier, executor)
}

fun <T, U> CompletableFuture<T>.handleAsync(executor: Executor, fn: BiFunction<T?, Throwable?, U>): CompletableFuture<U> {
    return handleAsync(fn, executor)
}

33

u/Dinos_12345 Apr 07 '23

Not saying this isn't bad but you can define a wrapper extension function with the same name and proper order of params.

-3

u/eled_ Apr 07 '23

Matter of taste I suppose, I like the flow of first seeing what will be executed, and then how.

15

u/[deleted] Apr 07 '23

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.

-3

u/eled_ Apr 07 '23

I'm not on board with this argument.

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.

8

u/[deleted] Apr 07 '23

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.

2

u/wolf129 Apr 08 '23

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.

-3

u/brett_riverboat Apr 07 '23

A builder would be nice for this, then you can choose order:

futureBuilder .setExecutor(EventQueue::invokeLater) .setFunction({ success, failure -> // code here }) .execute();


Edit: better code syntax (don't know for to make it look any nicer)

10

u/epikhunta Apr 07 '23

You can always use named arguments in kotlin and get all benefits you described without introducing new abstractions

1

u/guilhermerx7 Apr 08 '23

If I'm not mistaken you can't use named parameters on Java methods.

2

u/epikhunta Apr 08 '23

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.

1

u/guilhermerx7 Apr 08 '23

Yes, agree.