fix: OfType now accepts a collection of object? by amis92 · Pull Request #1617 · dotnet/reactive (original) (raw)

What is the nature of your contribution?

Bugfix

When using OfType<T> on an IAsyncEnumerable<T?> the compiler raised CS8620 warning:

Argument of type 'IAsyncEnumerable<object?>' cannot be used for parameter 'source' of type 'IAsyncEnumerable<object>'
in 'IAsyncEnumerable<object> AsyncEnumerable.OfType<object>(IAsyncEnumerable<object> source)'
due to differences in the nullability of reference types.

The warning appeared in the unit test I've added, before I've changed OfType declaration.

The expected behavior is that the compiler doesn't raise a warning, because we're explicitly asking for our collection to be filtered for not-null objects (null has no type in terms of type checks).

The fix is to change the nullability of type parameter for extension method receiver (this IAsyncEnumerable<object>) to be nullable (object?) instead.

This PR fixes #1525, which was the original report of the above issue.