-
Notifications
You must be signed in to change notification settings - Fork 88
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
base: draft-v8
Are you sure you want to change the base?
Conversation
…rays of nullable.
standard/types.md
Outdated
: non_nullable_array_reference_type nullable_type_annotation | ||
; | ||
|
||
non_nullable_array_reference_type |
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.
I find this name confusing. It's about what might be an element of an array, right?
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.
@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!
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.
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 :)
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.
I also find "array reference type" very confusing, and would prefer element
in it regardless of length.
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.
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.
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.
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.
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.
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`. | ||
|
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?
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 |
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.
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 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.
Adds support for nullable arrays, arrays of nullables, and nullable arrays of nullables.
Review notes:
arrays.md
,types.md
,sample.cs
&ReadMe.md
need to be considered; all other files are part of the testing machinery.sample.svg
file to understand the parse the grammar generates.