Skip to content
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

Add support for nullable arrays nullables #1287

Draft
wants to merge 3 commits into
base: draft-v8
Choose a base branch
from

Conversation

Nigel-Ecma
Copy link
Contributor

Adds support for nullable arrays, arrays of nullables, and nullable arrays of nullables.

Review notes:

  • Only arrays.md, types.md, sample.cs & ReadMe.md need to be considered; all other files are part of the testing machinery.
  • If you find it helpful you can download and view the sample.svg file to understand the parse the grammar generates.

@Nigel-Ecma Nigel-Ecma added the meeting: discuss This issue should be discussed at the next TC49-TG2 meeting label Mar 11, 2025
@Nigel-Ecma Nigel-Ecma added this to the C# 8.0 milestone Mar 11, 2025
@Nigel-Ecma Nigel-Ecma self-assigned this Mar 11, 2025
: non_nullable_array_reference_type nullable_type_annotation
;

non_nullable_array_reference_type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this name confusing. It's about what might be an element of an array, right?

Copy link
Contributor Author

@Nigel-Ecma Nigel-Ecma Mar 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jskeet – yes it is about the element type. The new array grammar follows the form of the existing non-array grammar with each of the former corresponding with one of the latter:

non_array_type & type
array_reference_type & reference_type
non_nullable_array_reference_type & non_nullable_reference_type
nullable_array_reference_type & nullable_reference_type

I did originally consider non_array_reference_type, as in:

non_array_type & type
non_array_reference_type & reference_type
non_nullable_non_array_reference_type & non_nullable_reference_type
nullable_non_array_reference_type & nullable_reference_type

but went with the shorter option. Happy to add the non_ in if folk think it helps understanding. Other suggestions can be submitted as well!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was most confused by it being called "array type" when it's not about the array so much as the elements of the array. But adding in "element" would make it significantly longer. Will have a mull - and look closely again having confirmed it does mean what I expected it to mean :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also find "array reference type" very confusing, and would prefer element in it regardless of length.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I see what's happening. Given that these types may not appear as an element of an array, I retract my desire for element to be in the name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

non_array_non_nullable_reference_type gets across: "this is the full set of C# types which are not arrays and which are non-nullable reference types." It also gets rid of the confusion about whether this includes arrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rules now renamed to the longer versions above.

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

Copy link
Contributor

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?

string[][]
string?[][]
string[]?[]
string[][]?
string?[]?[]
string?[][]?
string[]?[]?
string?[]?[]?

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?


At run-time, a value of an array type can be:

- `null`; or
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Nigel-Ecma Nigel-Ecma marked this pull request as ready for review March 19, 2025 20:58
@Nigel-Ecma Nigel-Ecma marked this pull request as draft March 19, 2025 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
meeting: discuss This issue should be discussed at the next TC49-TG2 meeting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants