Skip to content

Instantly share code, notes, and snippets.

@Takhion
Forked from rock3r/RxFilterIsInstance.kt
Last active June 29, 2020 17:04
Show Gist options
  • Save Takhion/49e21493e9be13627fc3383499abaf60 to your computer and use it in GitHub Desktop.
Save Takhion/49e21493e9be13627fc3383499abaf60 to your computer and use it in GitHub Desktop.
Port Kotlin's awesome filterIsInstance() to RxJava — one call to filter by type and map-cast to that type
package me.seebrock3r.extensions.reactivex
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
@Suppress("UNCHECKED_CAST") // The cast happens after we filter by type so it's safe
inline fun <reified R> Flowable<*>.filterIsInstance(): Flowable<R> =
filter { it is R }
@Suppress("UNCHECKED_CAST") // The cast happens after we filter by type so it's safe
inline fun <reified R> Observable<*>.filterIsInstance(): Observable<R> =
filter { it is R }
@Suppress("UNCHECKED_CAST") // The cast happens after we filter by type so it's safe
inline fun <reified R> Single<*>.filterIsInstance(): Maybe<R> =
filter { it is R }
@Suppress("UNCHECKED_CAST") // The cast happens after we filter by type so it's safe
inline fun <reified R> Maybe<*>.filterIsInstance(): Maybe<R> =
filter { it is R }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment