[Proposal]: Nullable ref #8078
Unanswered
hez2010
asked this question in
Language Ideas
Replies: 2 comments
-
See dotnet/roslyn#72165 and dotnet/roslyn#71487:
|
Beta Was this translation helpful? Give feedback.
0 replies
-
See: #497 |
Beta Was this translation helpful? Give feedback.
0 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.
Uh oh!
There was an error while loading. Please reload this page.
-
Nullable ref
Motivation
We are consistently improving low-level struct experience and introduced
ref field
to the language to allow developers usingref
safely in the language.While it's possible to introduce yet another billion-dollars mistake that we are treating all
ref
s to be notnull
despite aref
can benull
in the code.Thinking about the following code:
This code will compile without a single line of warnings. If users use the code like this:
A
NullReferenceException
will be thrown at runtime. This has no difference than the world where we didn't have nullable reference type in the language.As of today (April 25, 2024), a simple search on the GitHub showing that there're already some libraries start to use at least
Unsafe.NullRef
in the code:As developers around the world upgrading their language version to the version that has ref fields, the adoption will increase.
To prevent such a potential billion-dollars mistake in the future, I propose to add nullable ref to the language.
Proposal
Nullable ref
We need to add nullable reference to the language. To do this, we can simply put a
?
next to theref
keyword:Now let's use the above type with following code:
As a sequence, a non-nullable ref type will require initialization, otherwise a warning will be produced:
We may suppress the warning if all the non-default constructors assign a valid ref to the field:
To enforce the null safety, any
default
initobj to a struct containing non-nullableref field
will produce a warning:For generics allowing ref structs, a warning will be produced for any substitution of a ref struct containing non-nullable refs:
Keyword for null ref
To allow assigning a null reference to a nullable ref type, we can introduce a new keyword
nullref
to the language to be a safe alternative toUnsafe.NullRef<T>()
:Why not
null
?null
is interpreted as an r-value, whilenullref
is a l-value, we want to distinguish them.Alternative: use
ref null
for this:Comparing with a
ref
withnullref
We can use
ref x == nullref
,ref x != nullref
, and mayberef x is nullref
andref x is not nullref
as well, to check whether aref
is a null reference or not.Or maybe simply use it without the
ref
, as thenullref
already contains the semantic ofref
:Or with
ref null
:Control-flow based nullable reference analysis
We need to introduce control-flow based nullable reference analysis for
ref?
as well, the rules can follow the same rules we have today for nullable reference types.We may want to mirror the nullable reference type code analysis related attributes as well:
AllowNull
:AllowNullRef
DisallowNull
:DisallowNullRef
MaybeNull
:MaybeNullRef
NotNull
:NotNullRef
MaybeNullWhen
:MaybeNullRefWhen
NotNullWhen
:NotNullRefWhen
NotNullIfNotNull
:NotNullRefIfNotNullRef
MemberNotNull
:MemberNotNullRef
MemberNotNullWhen
:MemberNotNullRefWhen
Open questions
ref field
is still not that wide, we may accept a small source breaking change here.Beta Was this translation helpful? Give feedback.
All reactions