You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Please consider the following example, where a Mono shall produce a lookup result or nothing if map.get() returns null.
Mono.just(mapOf("foo" to "bar")).mapNotNull { it["foo"] }
// produces Mono<String?> but should be Mono<String> instead
The Kotlin compiler could know that the inner type must be non-null.
Project Reactor's code does not seem to signal any @NonNull.
Reactor-kotlin-extensions does not seem to extend the mapNonNull() operator.
Desired solution
The mapNotNull operator should produce a Mono<X> instead of Mono<X?>.
Reactor-kotlin-extensions could override Mono / Flux mapNotNull() to hint the correct type to the kotlin compiler.
Considered alternatives
Currently I just Mono.cast<> the result to non-null, which is suboptimal and requires knowledge of the correct class.
With Kotlin 1.7's new definitely not null types, one could just do:
fun <R, T>Mono<T>.mapNotNullTyped(mapper: (T) ->R) : Mono<R & Any> {
returnthis.mapNotNull(mapper) asMono<R & Any> //rely on mapNotNull not to produce null, else throw
}
Update: This simple implementation works on my machine:
I could not help but wonder, why the compiler accepts this:
privatefun <R, T> Mono<T>.mapNotNullTyped2(mapper: (T) ->R?): Mono<R> {
val foo =this.mapNotNull<R>(mapper) //accepts Mono<R> although mapper: (T) -> R? without a castreturn foo
}
So I found the following code works as well, if you specify the type.
Mono.just(mapOf("foo" to "bar"))
.mapNotNull<String> { it["foo"] } //produces Mono<String!> instead of Mono<String?>
As there is no explicit cast anywhere, and I don't think the compiler can deduce non-null from the code, this would just be java-kotlin interop, as kotlin treats the String! as a platform type and simply disables null safety.
Motivation
Please consider the following example, where a Mono shall produce a lookup result or nothing if map.get() returns
null
.The Kotlin compiler could know that the inner type must be non-null.
Project Reactor's code does not seem to signal any
@NonNull
.Reactor-kotlin-extensions does not seem to extend the mapNonNull() operator.
Desired solution
The mapNotNull operator should produce a
Mono<X>
instead ofMono<X?>
.Reactor-kotlin-extensions could override Mono / Flux
mapNotNull()
to hint the correct type to the kotlin compiler.Considered alternatives
Currently I just
Mono.cast<>
the result to non-null, which is suboptimal and requires knowledge of the correct class.With Kotlin 1.7's new definitely not null types, one could just do:
Update: This simple implementation works on my machine:
Additional context
Tested with
Thank you very much
Martin
The text was updated successfully, but these errors were encountered: