Skip to content

Fix warning when using nullable reference types with OfType operator #1525

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

Closed
pellet opened this issue Apr 22, 2021 · 2 comments · Fixed by #1617
Closed

Fix warning when using nullable reference types with OfType operator #1525

pellet opened this issue Apr 22, 2021 · 2 comments · Fixed by #1617

Comments

@pellet
Copy link

pellet commented Apr 22, 2021

Bug

Despite our best efforts, bugs can slip into releases or corner cases forgotten about. We will try our best to remedy the situation
and/or provide workarounds. Note that certain (odd) behaviors are by design and as such are not considered bugs.

Which library version?
v5.0

What are the platform(s), environment(s) and related component version(s)?
.net5.0

What is the use case or problem?
I want to use the OfType operator for filtering out nulls like so:

Observable
    .Return<string?>("blah")
    .OfType<string>()
    .SelectMany(this.audioService.PlayAudioPair)
    .Subscribe();

What is the expected outcome?
No warnings

What is the actual outcome?
When compiled has a warning:
warning CS8620: Argument of type 'IObservable<string?>' cannot be used for parameter 'source' of type 'IObservable<object>' in 'IObservable<string> Observable.OfType<string>(IObservable<object> source)' due to differences in the nullability of reference types.

@pellet
Copy link
Author

pellet commented Apr 23, 2021

I've found the operator .WhereNotNull() is working for me without warnings, but it is from ReactiveUI, it would be nice to have the OfType() operator handling nulls too or something similar in RX if possible.

@koenigst
Copy link

Why was this only fixed for IAsyncEnumerable even though the original request was for IObservable?

object?[] items = [1, null];
var notNullAe = items.ToAsyncEnumerable().OfType<object>();
var notNullObs = items.ToObservable().OfType<object>();

The observable version results in warning CS8620: Argument of type 'IObservable<object?>' cannot be used for parameter 'source' of type 'IObservable<object>' in 'IObservable<object> Observable.OfType<object>(IObservable<object> source)' due to differences in the nullability of reference types. while the async enumerable does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants