New custom attribute based on MaybeNullWhen that checks a named out parameter (not just the return value) #9591
Unanswered
jonopare
asked this question in
Language Ideas
Replies: 1 comment 3 replies
-
The problem is that the compiler doesn't implement the necessary aliasing analysis that could enable this to work, so you'd have to start by convincing us that is worth the (considerable) effort. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I personally find that the
MaybeNullWhenAttribute
is very useful with nullable reference types. When a method that returnsbool
is called, the compiler can check if the return value was true (or false) and then infer whether some nullability constraints have been satisfied (or not).For example:
can be used like this without worrying whether message is null:
However, the
void Deconstruct(out bool, out object)
pattern doesn't have a boolean return value, and therefore can't make use of theMaybeNullWhenAttribute
.I'd like to propose (if it hasn't already been proposed and I just couldn't find the discussion) a new attribute:
It would enable the following deconstruction to not have to perform null checking when success is true:
For me, this would be preferable to changing the signature of Deconstruct to return a
out object? value
, and subsequently having to perform the null checks myself (or worse, use the!
operator.)Beta Was this translation helpful? Give feedback.
All reactions