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
Apply NotNullIfNotNull with multiple arguments #43906
Comments
Had a situation like this last week, but I didn't write it down. :-( |
I have a use case: public static T Min<T>([AllowNull] T left, [AllowNull] T right)
where T : IComparable<T>
=> left == null ? (right == null ? default : (right.CompareTo(left) > 0 ? left : right)) : (left.CompareTo(right) < 0 ? left : right); If either |
@wilhelmzapiain That method would confuse me. I'd expect Enumerable.Min semantics: "the min of everything that isn't null" |
Alternate option: dotnet/csharplang#6981 |
Maybe but if you want to write a null-safe Plus operator in analogy to SQL semantics, you have a perfectly valid example for this issue. Either a solution with... [return: MayBeNullIfNull(nameof(left)), MayBeNullIfNull(nameof(right))] // with OR semantics
public static Value operator +(Value? left, Value right) {} ... or ... [return: NotNullIfNotNull(nameof(left), nameof(right))] // with AND semantics
public static Value? operator +(Value? left, Value right) {} ... would be highly appreciated also for our project where we have such operators used in really many places and refactoring all of them out to fully apply nullable reference types would be hell. |
Currently multiple
NotNullIfNotNull
behaves as if they are being OR'edIf this is the intended behavior, a possible solution is to permit multiple arguments on the attribute
So that we get a warning if either of arguments are possible nulls.
The text was updated successfully, but these errors were encountered: