[Proposal]: NotNullWhen
on return values
#6932
Replies: 9 comments 7 replies
-
Roslyn internally tends to use a name like |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Many thanks for this proposal. I'm waiting for this too. FYI, some people consider that this boolean is a code smell and should be avoided (by using 2 different methods). My humble opinion is that even if it could be done, this inevitably leads to I understand the "code smell" but for low-level API, the boolean helps a lot (at least for me). |
Beta Was this translation helpful? Give feedback.
-
@olivier-spinelli I am pretty sure that the |
Beta Was this translation helpful? Give feedback.
-
For what is worth, this can be accomplished today with public Thing? GetThing(string name, [DoesNotReturnIf(true)] bool throwIfNotFound = true) { ... } |
Beta Was this translation helpful? Give feedback.
-
@dubiousconst282 I think you are misunderstanding something: The function will of course return if it executed successfully, even if the boolean parameter is true. This parameter only has effect in the case where the function failed to execute: It specifies whether the function will return then or not. |
Beta Was this translation helpful? Give feedback.
-
Even just simple:
Would be a huge improvement and probably easier to implement than the more general |
Beta Was this translation helpful? Give feedback.
-
This would be amazingly helpful as it would allow better support for methods where you either throw an exception if the item is not found or return null based on a parameter. I think it would make sense to me for the attribute to be used like the DoesNotReturnIfAttribute i.e. NotNullIf on a parameter so that could be used like this. internal static string? GetAttribute(this XmlReader reader, string attributeName, [NotNullIf(true)] bool throwIfNotFound = true) {
string? attributeValue = reader.GetAttribute(attributeName);
if (throwIfNotFound && attributeValue is null)
throw new Exception($"Attribute '{attributeName}' not found");
return attributeValue;
} |
Beta Was this translation helpful? Give feedback.
-
I've often thought that something like this exists, went to documentation only to remember that it doesn't. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
NotNullWhen
on return valuesSummary
The proposed usage of the existing compiler attribute
NotNullWhen
allows declaring a nullable return value as not-null when a boolean parameter has the specified value.Motivation
In the following example, the returned
XmlNode
may benull
if the given XPath is not found. If the boolean parameteri_throw
istrue
, the function will never returnnull
, but throw an exception instead. There is no way to tell this to the compiler.With the proposed attribute usage, the compiler would know the behaviour of the function and it would not mark the variable in the screenshot as "may be null".
Detailed design
I am not an expert on C# language, so I don't know how the existing implementation must be enhanced or modified, if at all.
Someone else may
Drawbacks
Maybe the benefit is not worth the effort.
But I don't see any language-related issues.
Alternatives
The alternative, which I already use, is creating an overload of the function with return value
XmlNode
instead ofXmlNode?
:I would like to avoid this extra work.
Unresolved questions
Design meetings
Beta Was this translation helpful? Give feedback.
All reactions