0

I am trying to create an Rx operator that works on arrays. I've tried this simple extension:

extension ObservableType where Element == Array<Any> {
    func beat<U>(_ beat: Observable<U>) -> Observable<Element.Element> {
        let lhs = self.flatMap { Observable.from($0) }
        return Observable.zip(lhs, beat).map { $0.0 }
    }
}

Now, I've noted Array<Any> because the otherwise I get Reference to generic type 'Array' requires arguments in <...>, but this way the return value is of courses Observable<Any>.

Is it even possible to return an observable with the actual Element of the array?

Yotam
  • 9,789
  • 13
  • 47
  • 68

2 Answers2

1

How about this?

    func beat<U>(_ beat: Observable<[U]>) -> Observable<[U]> where [U] == Element {
        Observable.zip(self, beat).map { $0.0 }
    }
pchmelar
  • 13
  • 1
  • 4
0

Most likely you want to use the parametrized extensions that is not implemented in Swift 5.2.

As a workaround, you can create a global generic function that accepts Array<T> as the second parameter.

Roman Podymov
  • 4,168
  • 4
  • 30
  • 57