Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
Call the same function with two different types from a when condition

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
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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()
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda