Skip to content

Proposal: filterBy(Class) API #4089

Closed
Closed
@ZacSweers

Description

@ZacSweers

When dealing with polymorphic types, I've often had cases where I needed to filter by a type and then immediately have to follow that with a seemingly-redundant cast to that type that I've already filtered on. This issue proposes an operator that would add a convenience API for this.

Example:

myMammalObservable
    .filter(m -> m instanceof Dog)
    .cast(Dog.class)
    .subscribe(dog -> dog.playFetch());

Proposal would probably encapsulate that exact logic, though we could combine it into a single operator if there's a perf gain to be had by doing so. Would look like this in use:

myMammalObservable
    .filterBy(Dog.class)
    .subscribe(dog -> dog.playFetch());

Up for better naming suggestions, just found myself doing this several times in the last week. Totally understand if it's trivial enough not to warrant something on the first-party API. Happy to submit a PR if you'd want it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions