-
Notifications
You must be signed in to change notification settings - Fork 456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
combineLatest/zip array overload to be typesafe #173
Comments
In practice this will be the exact same thing as the generic parameter will be widened to |
It is explained in the JavaDoc:
As the |
Man you guys are fast lol. @JakeWharton I will admit it doesn't help when T is widened. My specific use case was using it like this. (I changed the name of some types) combineLatest(viewModelRequests) { requests ->
val loading = request.any { it is Request.Loading }
val items = request.mapNotNull { it.successDataOrNull() }
.flatmap{ it } // the type of "it" is List<ListItemViewModel>
return@combineLatest ViewModel(loading, items)
} I have an android recycler view that is populated from many sources and I combine them all as a list. It was easier then having the overload that took five observables. I basically had a list that I would unpack for combineLatest then immediately repack it back into a list. @akarnokd Kotlin does have the same restrictions for non inline methods. For inline methods, you can make the type |
This does the trick: inline fun <reified T> Iterable<Single<T>>.zip(): Single<List<T>> {
return Single.zip(this) {
it.filterIsInstance<T>()
}
} |
I wanted to use the version of
combineLatest
that takes an array/iterable. The only problem is that it gives you aArray<Any>
which wasn't very useful. I thought it was kind of weird that RxJava doesn't have a version that gives youArray<T>
. Was this because of the Java compiler couldn't figure out the two overloads? Anyway, would we want to provide a version in this project that correctly has the typeT
for the array? Maybe there is a good reason not to, but I found it really useful for what I was working on.I made one myself by using both Kotlin and Java together to do what I think is the most efficient version while still being useful.
This uses Kotlin to actually create the array we are copying to. We can do this because the type is reified. The java then uses the
Arrays.asList
to create a simple list wrapper for the array and then copies it into the array we made. So we make one copy of the array and a list wrapper for the array.The text was updated successfully, but these errors were encountered: