springspring-webfluxproject-reactor

Why does flatMapMany exist as a standalone method instead of an overload of flatMap?


I was confused by this and it wasn't addressed by the documentation. To me, it looks like the need for FlatMapMany can be inferred by the compiler from the types involved (if a flatMap on a Mono returns a Flux). Why have a separate method at all? Why not simply have flatMap overloaded to handle this complexity for developers. Is it not possible because of the underlying implementation?

To be clear, I come from a functional background (Scala) and am pretty good with most of the concepts involved, but it seems to me looking at Reactor and WebFlux that many of the constructs could be expressed in a much more user-friendly way without losing any functionality whatsoever. Am I missing something?


Solution

  • I think this is an opinionated choice. Reactor prefer to be strict and to conserve the same type between input and output for common functional operators like "map" and "flatMap".

    This is explained on Mono class javadoc:

    The rx operators will offer aliases for input Mono type to preserve the "at most one" property of the resulting Mono. For instance flatMap returns a Mono, while there is a flatMapMany alias with possibly more than 1 emission.