I have some type system head-scratching trying to figure it out. I want to wrap Java 7 Future into kotlin’s Deferred. There is a Java 8
CompletableFuture.asDeferred()
already, but when the API you’re using is still java7-compatible, tough luck.
So far I just went with
inline fun <reified T> Future<T>.asCompletableDeferred(): Deferred<T> =
CompletableFuture.supplyAsync { this.get() }.asDeferred()
and it works, as far as I can see.
But I’d like to omit unnecessary wrapping if
this
is already Completable:
inline fun <reified T> Future<T>.asCompletableDeferred(): Deferred<T> =
if (this is CompletionStage<*>) this.asDeferred() else CompletableFuture.supplyAsync { this.get() }.asDeferred()
and that is going all hysterical complaining that
this.asDeferred()
is no longer
Deferred<T>
but
Deferred<Any>
Why is that so and how to fix it?