Search code examples
javavavr

Can I explicitly type an ambiguous method call and use it?


I'm sure this is answered somewhere, but I don't have good enough search terms to find it. I am using the io.vavr.control.Try package, and when I try to use getOrElseThrow method on an element in a stream of results, the method is ambiguous with the io.vavr.Value class. Can I specify which method I want to use somehow, or is it impossible?

enter image description here


Solution

  • You have a number of options:

    • Add an explicit cast to the desired type:

      .map(rsp -> rsp.getOrElseThrow((Supplier<NotFoundException>) NotFoundException::new))
      .map(rsp -> rsp.getOrElseThrow((Function<? super Throwable, NotFoundException>) NotFoundException::new))
      
    • Use a lambda expression instead of a method reference:

      .map(rsp -> rsp.getOrElseThrow(() -> new NotFoundException()))
      .map(rsp -> rsp.getOrElseThrow(t -> new NotFoundException(t)))
      
    • Use an explicit type of the outer lambda parameter:

      .map((Value<…> rsp) -> rsp.getOrElseThrow(NotFoundException::new))
      .map((Try<…> rsp) -> rsp.getOrElseThrow(NotFoundException::new))