0

Note: This problem is same for Observable, with more variations like combineLatest. So I've picked Single as the minimal example.

I routinely encounter situations where there is a list of sources of same type and they all must emit before continuing. I resolve it with zip:

Single.zip(sources) { it.map { it as SomeType } }

This is fragile because the Single.zip(sources, zipper) is not type-safe so I have to coerce elements on the return value. Isn't there a built-in operator to do that?

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
Agent_L
  • 4,960
  • 28
  • 30
  • No. It is not possible with Java's typesystem. – akarnokd Mar 11 '22 at 10:58
  • @akarnokd It's not possible only because of the `Object[]` instead of `List`. Coercing `zip` to be type-safe is trivial in Kotlin: `inline fun zipToList(sources: Iterable>): Single> = Single.zip(sources) { it.map { it as T } }`. I'm asking about alternatives to zip. – Agent_L Mar 11 '22 at 13:58
  • 1
    There is no such operator that would do that. There were attempts at rectifying the problem but none of the proposed solution worked well enough. In kotlin though, you can do workarounds more conveniently. – akarnokd Mar 11 '22 at 15:00
  • `[system.reactive]` is a .NET namespace. It's not a Java Rx tag. – Enigmativity Mar 15 '22 at 05:24
  • @Enigmativity Question is about Rx in general, not a particular implementation. – Agent_L Mar 15 '22 at 13:24
  • @Agent_L - Yes, which is why I removed the `[system.reactive]` tag. – Enigmativity Mar 16 '22 at 01:30
  • @Enigmativity So why haven't you removed the [rx-java] as well ? – Agent_L Mar 16 '22 at 10:56
  • @Agent_L - Only because I didn't know what language the code in the question was written in. I know it's not C# though. – Enigmativity Mar 16 '22 at 11:44

0 Answers0