https://kotlinlang.org logo
#coroutines
Title
# coroutines
i

Igor Kolomiets

06/21/2022, 5:45 PM
Hello, can you please advise on good naming convention for extension functions that convert existing Java API based on CompletableFuture to Kotlin’s suspendible variant? For example, there is Java service:
Copy code
public interface FibonacciService {
    CompletableFuture<Long> nextFibonacci(Long number);
}
In my Kotlin code, I’d like to add extension function to this interface to expose
nextFibonacci
method as suspended function:
Copy code
suspend fun FibonacciService.nextFibonacci(number: Long): Long = nextFibonacci(number).asDeferred().await()
Unfortunately, the code above doesn’t work, because
nextFibonacci
extension is shadowed by interface original method (I have to rename it to something different, e.g.
coNextFibonacci
,
suspndNextFibonacci
,
deferredNextFibonacci
?). Is there a conventional prefix (or suffix) to distinguish suspendible variants? Also, would it be nice if Kotlin language allowed for
suspend
variants in cases like this (presence of CoroutineScope would allow to figure out which one to use)?
e

ephemient

06/21/2022, 6:10 PM
do you need it? you can
CompletableFuture.await()
directly
👍 1
i

Igor Kolomiets

06/21/2022, 8:04 PM
Actually, with
CompletableFuture.await()
as of right now, I don’t need it. Thank you! Still, I guess, should there be a need to do something special with CoroutineContext or exception handling a dedicated
suspend
function may be required.
t

tseisel

06/22/2022, 5:25 PM
Specifying a different name with
@JvmName
should do the trick
i

Igor Kolomiets

06/22/2022, 8:20 PM
@tseisel interesting. I just checked your proposal to use
@JvmName
- it didn’t work (inside CoroutineContext, original method that returns
CompletableFuture<Long>
is resolved, not the extension with the same name).
a

andylamax

06/23/2022, 9:29 PM
yet another use case of
@LowPriorityOnOverideResolutionAmbiguity
7 Views