-
Notifications
You must be signed in to change notification settings - Fork 88
Add support for nullable arrays nullables #1287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: draft-v8
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ The element type of an array can itself be an array type ([§17.2.1](arrays.md#1 | |
> | ||
> <!-- Example: {template:"code-in-main-without-using", name:"PascalArrayDeclarations"} --> | ||
> ```csharp | ||
> int[][] pascals = | ||
> int[][] pascals = | ||
> { | ||
> new int[] {1}, | ||
> new int[] {1, 1}, | ||
|
@@ -46,9 +46,18 @@ In effect, the *rank_specifier*s are read from left to right *before* the final | |
|
||
> *Example*: The type in `T[][,,][,]` is a single-dimensional array of three-dimensional arrays of two-dimensional arrays of `int`. *end example* | ||
|
||
At run-time, a value of an array type can be `null` or a reference to an instance of that array type. | ||
An array type is a reference type and therefore can be a nullable reference type ([§8.9.3](types.md#893-nullable-reference-types)). Likewise the element type of an array can be a nullable reference type or a nullable value type ([§8.3.12](types.md#8312-nullable-value-types)): | ||
|
||
> *Note*: Following the rules of [§17.6](arrays.md#176-array-covariance), the value may also be a reference to a covariant array type. *end note* | ||
- An array type of the form `T[R]` is a non-nullable array with rank `R` and a non-array non-nullable element type `T`. | ||
- An array type of the form `T[R]?` is a nullable array with rank `R` and a non-array non-nullable element type `T`. | ||
- An array type of the form `T?[R]` is a non-nullable array with rank `R` and a non-array nullable element type `T`. | ||
- An array type of the form `T?[R]?` is a nullable array with rank `R` and a non-array nullable element type `T`. | ||
|
||
At run-time, a value of an array type can be: | ||
|
||
- `null`; or | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether it would be useful to have a note either here or in array creation expressions to warn that code that looks safe still can easily be null-unsafe with arrays: string[] x = new string[5];
int y = x[0].Length; ... no warnings, but will fail at run-time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jskeet – Well there were no warnings from the current version of whatever compiler you used, but there is not reason why some compiler couldn’t issue a warning… I suggest you spin this off as a seperate issue. |
||
- a reference to an instance of that array type; or | ||
- a reference to an instance of a covariant array type, if by [§17.6](arrays.md#176-array-covariance) such exists. | ||
|
||
### 17.2.2 The System.Array type | ||
|
||
|
@@ -132,15 +141,15 @@ Because of array covariance, assignments to elements of reference type arrays in | |
> ```csharp | ||
> class Test | ||
> { | ||
> static void Fill(object[] array, int index, int count, object value) | ||
> static void Fill(object[] array, int index, int count, object value) | ||
> { | ||
> for (int i = index; i < index + count; i++) | ||
> { | ||
> array[i] = value; | ||
> } | ||
> } | ||
> | ||
> static void Main() | ||
> static void Main() | ||
> { | ||
> string[] strings = new string[100]; | ||
> Fill(strings, 0, 100, "Undefined"); | ||
|
@@ -169,7 +178,7 @@ array_initializer | |
variable_initializer_list | ||
: variable_initializer (',' variable_initializer)* | ||
; | ||
|
||
variable_initializer | ||
: expression | ||
| array_initializer | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Sample: Arrays of NRT | ||
|
||
Samples for nullable arrays, arrays of nullables, and nullable arrays of nullables. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens with arrays of arrays? For example, are all of the following valid - and more importantly, are they all correctly described here?
It looks like they're all allowed, and they do make sense. I suspect it all just drops out in terms of the grammar - but I wonder whether just a single example (maybe
string?[][]?
) would be useful in a note?