Home > Net >  Call the same function with two different types from a when condition
Call the same function with two different types from a when condition

Time:02-13

I was wondering if it was possible to group a call to a function that is present for two different types, without having to create two branches in the "when" statement.

For example, in my case I have this class extensions:

// File: PublisherExtensions.kt

fun <T> Mono<T>.toServiceResponse(): Mono<Response<T>> =
    this.map { r -> Response(true, r, null) }
        .onErrorResume { e -> Mono.just(Response(false, null, Response.Error(500, e.message))) }

fun <T> Flux<T>.toServiceResponse(): Mono<Response<List<T>>> =
    this.collectList()
        .map { r -> Response(true, r, null) }
        .onErrorResume { e -> Mono.just(Response(false, null, Response.Error(500, e.message))) }

And what I use with them is this statement:

val body = when (val value = result.returnValue) {
    is Mono<*> -> value.toServiceResponse()
    is Flux<*> -> value.toServiceResponse()
    else -> throw RuntimeException("The \"body\" should be Mono<*> or Flux<*>!")
}

While what I would like would be this:

val body = when (val value = result.returnValue) {
    is Mono<*>, is Flux<*> -> value.toServiceResponse()
    else -> throw RuntimeException("The \"body\" should be Mono<*> or Flux<*>!")
}

The IDE gives me this error:

Unresolved reference.
None of the following candidates is applicable because of receiver type mismatch:

  • public fun Flux<TypeVariable(T)>.toServiceResponse(): Mono<Response<List<TypeVariable(T)>>> defined in brc.studybuddy.backend.wrapper.util in file PublisherExtensions.kt
  • public fun Mono<TypeVariable(T)>.toServiceResponse(): Mono<Response<TypeVariable(T)>> defined in brc.studybuddy.backend.wrapper.util in file PublisherExtensions.kt

CodePudding user response:

Notice that the second toServiceResponse can be defined in terms of the first:

fun <T> Flux<T>.toServiceResponse(): Mono<Response<List<T>>> =
    this.collectList().toServiceResponse()

So you are almost doing the same thing on Monos and Fluxes, except that for Fluxes, you also call collectList first.

val body = when (val value = result.returnValue) {
    is Mono<*> -> value
    is Flux<*> -> value.collectList()
    else -> throw RuntimeException("The \"body\" should be Mono<*> or Flux<*>!")
}.toServiceResponse()

Alternatively, without a when:

val body = result.returnValue.let { value ->
    (value as? Flux<*>)?.collectList() 
    ?: (value as? Mono<*>)
    ?: throw RuntimeException("The \"body\" should be Mono<*> or Flux<*>!")
}.toServiceResponse()
  • Related