Treat pointers as nullable values #8322
Unanswered
IS4Code
asked this question in
Language Ideas
Replies: 0 comments
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.
Uh oh!
There was an error while loading. Please reload this page.
-
Situation
At the moment, nullable types in the language are reference types (
class
constraint) andNullable<T>
. With such types, you get the following:??
, to pick a fallback value if the operand isnull
.?.
,?[]
, and?.Invoke()
(sic), allowing early exit from an expression in case of anull
operand.null
first (automatically forNullable<T>
,#nullable enable
(warnings
) for NRTs).However, for pointers, despite them being "nullable" (having a
null
value), none of this is possible:There is no
?->
operator, and extensions are not allowed for pointers (why?) so?.
is not applicable either:You can null-conditionally call delegates, but not
delegate*
:Lastly, even with just
#nullable enable warnings
, you get warnings for obviouslynull
object references, but not for pointers:Nullability attributes on pointers are ignored:
Proposal
Even though pointers are considered "reference types" internally (at least by ECMA-335) and have a
null
value, they are not "nullable" as considered by C#. My proposal is therefore:??
and?[]
for pointers.?->
or?()
, or allow extension methods for pointers together with?.
.warnings
). In such situations, treat pointers as reference types as if#nullable enable warnings
was used (no type annotations or new syntax for "nullable pointers"), and extend attributes such asAllowNull
orNotNullIfNotNull
to pointers.Examples
Operators
Attributes
Beta Was this translation helpful? Give feedback.
All reactions