Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!