Skip to content
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

Cleanup ! operator usage in FallbackIfEmpty #808

Merged
merged 1 commit into from
Mar 22, 2021

Conversation

sid-6581
Copy link
Contributor

As part of reviewing nullable reference annotations (#803), this PR attempts to simplify usage of the ! operator in the internals of FallbackIfEmpty.cs. No public APIs are affected.

The following line from the code prior to this PR will cause a compiler warning CS8619 (nullability of reference types in source type doesn't match target type):

return FallbackIfEmptyImpl(source, 1, fallback, default!, default!, default!, null);

Because potential null values are passed to FallbackIfEmptyImpl, the return value will be IEnumerable<T?>.

One solution would be to use another ! at the end of the method call. However, the overuse of ! doesn't feel very clean. Also, the signature of FallbackIfEmptyImpl doesn't seem to signal intent as clearly as making the fallback values nullable. This PR also brings some consistency, since the fallback collection is already nullable.

Now that the fallback values are nullable, we can remove the ! from each of the defaults passed to FallbackIfEmptyImpl, but we need to use the ! operator on the fallback values when returning them. This seems a reasonable tradeoff to clean up this file, and should be safe considering the private accessibility of the method.

Copy link
Member

@atifaziz atifaziz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the signature of FallbackIfEmptyImpl doesn't seem to signal intent as clearly as making the fallback values nullable. This PR also brings some consistency, since the fallback collection is already nullable.

Completely agree. This is much better!

Thanks!


PS In #804, I introduced Defaultable<> that allows getting rid of ! completely but I think that's just being sneaky and I might revert.

@atifaziz atifaziz merged commit 7458bf1 into morelinq:master Mar 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants