Skip to content

Commit 7fb7b7e

Browse files
committed
Notes for next updates
1 parent a621bc7 commit 7fb7b7e

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

standard/types.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -720,23 +720,45 @@ An *unmanaged_type* is any type that isn’t a *reference_type*, a *type_paramet
720720

721721
### §Nullable-Types-General General
722722

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.
724724

725725
There are two forms of nullability for reference types:
726726

727727
- *nullable*: A *nullable-reference-type* can be assigned `null`. Its default null state is *maybe-null*.
728728
- *non-nullable*" A *non-nullable reference* should not be assigned a `null` value. Its default null state is *not-null*.
729729

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).
731731

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*
733733
734734
### §Non-nullable-reference-types Non-nullable reference types
735735

736736
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*.
737737

738738
> *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*
739739
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
759+
760+
-->
761+
740762
### §Nullable-reference-types Nullable reference types
741763

742764
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
747769

748770
#### §Nullable-Contexts-General General
749771

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*.
751773

752774
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.
753775

754776
The default state of the nullable context is implementation defined.
755777

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.
757779

758780
#### §Nullable-Disable-Context Nullable disable
759781

@@ -762,15 +784,15 @@ When the nullable context is ***disabled***:
762784
- A variable of any reference type is non-nullable.
763785
- No warning shall be generated when a variable of an unannotated reference type is initialized with, or assigned a value of, `null`.
764786
- 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`.
766788
> *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.
768790

769791
> *Example*:
770792
>
771793
> <!-- Example: {template:"code-in-main-without-using", name:"NullableAnnotationContext1", ignoredWarnings:["CS0219","CS8632"], expectedException:"NullReferenceException"} -->
772794
> ```csharp
773-
> #nullable disable
795+
> #nullable disable annotations
774796
> string? s1 = null; // Informational message; ? is ignored
775797
> string s2 = null; // OK; null initialization of a reference
776798
> s2 = null; // OK; null assignment to a reference

0 commit comments

Comments
 (0)