You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: standard/types.md
+30-8Lines changed: 30 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -720,23 +720,45 @@ An *unmanaged_type* is any type that isn’t a *reference_type*, a *type_paramet
720
720
721
721
### §Nullable-Types-General General
722
722
723
-
Any expression of a reference type can either refer to an object, or be the value `null`, which indicates "no reference". The terms "nullable reference type" and "non-nullable reference type" refer to annotations that declare whether an expression is intended to permit null values or not. A compiler can provide diagnostics when a expression is not used according to that intent.
723
+
Any expression of a reference type can either refer to an object, or be the value `null`, which indicates "no reference". The terms "nullable reference type" and "non-nullable reference type" refer to annotations that declare whether an expression is intended to permit null values or not. A compiler can provide diagnostics when a expression is not used according to that intent. The null state of an expression is defined in §Nullabilities-And-Null-States.
724
724
725
725
There are two forms of nullability for reference types:
726
726
727
727
-*nullable*: A *nullable-reference-type* can be assigned `null`. Its default null state is *maybe-null*.
728
728
-*non-nullable*" A *non-nullable reference* should not be assigned a `null` value. Its default null state is *not-null*.
729
729
730
-
Unlike with nullable value types, where value types `V` and `V?` denote different types, given a reference type `R`, the notations `R` and `R?` denote the same unannotated type, `R`; the difference in their notations indicates, at compile time, only the intent of their usage, and allows for static flow analysis. An identity conversion exists among a nullable reference type and its corresponding non-nullable reference type (§10.2.2).
730
+
Unlike with nullable value types, where value types `V` and `V?` denote different types, given a reference type `R`, the notations `R` and `R?` denote the same unannotated type, `R`; the difference in their notations indicates, at compile time, only the intent of their usage. An identity conversion exists among a nullable reference type and its corresponding non-nullable reference type (§10.2.2).
731
731
732
-
> *Note:* By definition, a reference type is nullable; that is, a variable of that type can either contain a reference to an object or be the value `null`, which indicates “no reference.” In a disabled nullable context, reference types aren't annotated. Therefore, they are non-nullable reference types. However, the disabled context means the compiler does not track null state and does not issue nullable related warnings. The disabled nullable context matches the previous standard behavior for reference types. *end note*
732
+
> *Note:* By definition, a reference type is nullable; that is, a variable of that type can either contain a reference to an object or be the value `null`, which indicates “no reference.” In a nullable context (§Nullable-Directives) where annotations are disabled, reference types aren't annotated. Therefore, they are non-nullable reference types. However, when the annotations and warnings flags are disabled, the compiler does not issue nullable related warnings. A nullable context where both flags are disabled matches the previous standard behavior for reference types. *end note*
A ***non-nullable reference type*** is a reference type of the form `T`, where `T` is the name of the type. A variable of a non-nullable reference type should not contain the value `null`. Warnings are generated when a variables of the type `T` is assigned to `null`. The default null-state of a non-nullable variable is *not-null*.
737
737
738
738
> *Note:* A non-nullable reference type is defined by the absence of the `?` annotation on the type declaration. When the nullable annotation context is disabled, `T` and `T?` both refer to the same types. *end note*
739
739
740
+
<!-- TO do notes
741
+
742
+
The "type" of an expression depends on the annotation flag where the expression is declared. The "type" is "nullable", "non-nullable", or "oblivious" (if we return to 3 types.)
743
+
744
+
The null-state of an expression depends on the state of the flags for that expression, and the types of each variable.
745
+
746
+
That last point is why an oblivious type may be needed.
747
+
748
+
My assignment: generate samples with all combinations, then analyze each one in terms of 2 types and 3 types so explain the behavior.
749
+
750
+
```csharp
751
+
#nullable disable
752
+
string M() => null;
753
+
754
+
# nullable enable
755
+
string s = M();
756
+
757
+
For all the combinations in both the declaring and accessing connotations.
758
+
And, include `var` in both enabled and disabled locations
A reference type of the form `T?` (such as `string?`) is a ***nullable reference type***. The annotation `?` indicates the intent that variables of this type are nullable. When the nullable annotation context (§Nullable-Annotation-Context) is enabled, the compiler shall recognize these intents. When the nullable annotation context is disabled, the compiler shall ignore these intents. When the nullable annotation context is disabled, a reference type of the form `T?` is the same as a reference of the form `T`.
@@ -747,13 +769,13 @@ A reference type of the form `T?` (such as `string?`) is a ***nullable reference
747
769
748
770
#### §Nullable-Contexts-General General
749
771
750
-
Every line of source code has a ***nullable context***. The nullable context controls whether nullable annotations (§Nullable-Annotation-Context) have effect and whether nullable warnings (§Nullable-Warning-Context) are issued by the compiler. The nullable context can be one of *disabled*, *enabled*, *annotations*, or *warnings*. The *enabled* setting combines both *annotations* and *warnings*.
772
+
Every line of source code has a ***nullable context***. The annotations and warnings flags for the nullable context control whether nullable annotations (§Nullable-Annotation-Context) are allowed and whether nullable warnings (§Nullable-Warning-Context) are issued by the compiler, respectively. Each flag can be *enabled*or *disabled*.
751
773
752
774
The nullable context may be specified within source code via nullable directives (§Nullable-Directives) and/or via some implementation-specific mechanism external to the source code. If both approaches are used, nullable directives supersede the settings made via an external mechanism.
753
775
754
776
The default state of the nullable context is implementation defined.
755
777
756
-
Throughout this specification, all C# code that does not contain nullable directives, or about which no statement is made regarding the current nullable context state, shall be assumed to have been compiled with nullable context enabled.
778
+
Throughout this specification, all C# code that does not contain nullable directives, or about which no statement is made regarding the current nullable context state, shall be assumed to have been compiled using a nullable context where both annotations and warnings are enabled.
757
779
758
780
#### §Nullable-Disable-Context Nullable disable
759
781
@@ -762,15 +784,15 @@ When the nullable context is ***disabled***:
762
784
- A variable of any reference type is non-nullable.
763
785
- No warning shall be generated when a variable of an unannotated reference type is initialized with, or assigned a value of, `null`.
764
786
- No warning shall be generated when a variable of a reference type that possibly has the null value.
765
-
- For any reference type `T`, the annotation `?` in `T?`is ignored. An informational message should be generated to that effect.
787
+
- For any reference type `T`, the annotation `?` in `T?`generates a message and the type `T?` is the same as `T`.
766
788
> *Note*: This message is characterized as “informational” rather than “warning,” so as not to confuse it with the state of the nullable warning setting, which is unrelated. *end note*
767
-
- The null-forgiving operator `!`is ignored.
789
+
- The null-forgiving operator `!`generates a message, and has no effect.
0 commit comments