-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Reword doc on undef arrays with missing #38260
Conversation
doc/src/manual/missing.md
Outdated
`Union{Missing, T}` creates an array filled with `missing`. If `T` is a | ||
singleton type the value that is used to fill the array is undefined and | ||
could change in the future, so it should not be relied upon. | ||
Due to implementation details, uninitialized arrays created with |
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.
maybe better say that this is the current behavior, but in general it should be treated as undefined for the purposes of the contract that the docstring gives. I thought this was your intention (with which I agree).
@nalimilan is probably better than me with suggesting a proper wording 😄.
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.
Yeah, definitely better to emphasize the "undefined"-ness, hence undef
, and that whatever is initialized shouldn't be relied on.
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.
Yes that was my intention 😊
I wanted to avoid "currently" because it implies that the behavior might change, but it's precisely the part mentioned here that won't change according to Jeff. I didn't want the documentation to include something misleading... On the other hand, a permanent truth is currently true so "currently" is technically correct.
I also thought it was useful to mention "due to implementation details", otherwise the reader would think "then why the heck do they initialize these values!".
Here are two proposals:
Due to implementation details, uninitialized arrays created with
`Array{Union{Missing, T}}(undef, dims)` or with `similar` are actually
filled with `missing` values in most cases where `T` is a bits type
(`isbitstype(T)` returns `true`). However, this behavior is specific to
`Base.Array` and is not reliable (it can fail when `T` is a singleton type).
Values returned by `undef` constructors should be treated as undefined. Use
a `missing` constructor as shown above to initialize values to `missing`.
and
In the current implementation, uninitialized `Base` arrays as created with
`Array{Union{Missing, T}}(undef, dims)` or with `similar` are actually
filled with `missing` values in most cases where `T` is a bits type
(`isbitstype(T)` returns `true`). This behavior should not be relied upon:
values returned by `undef` constructors should be treated as undefined. Use
a `missing` constructor as shown above to initialize values to `missing`.
I slightly prefer the second one. What do you think?
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.
probably core devs should pick here 😄
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.
OK let's wait for their input then!
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.
It should just say something like "using undef
may currently give an array filled with missing
, but this is not the correct way to obtain such an array. pass missing
to the constructor instead." There's no need to mention bits types etc., it just confuses the issue.
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.
@JeffBezanson thanks for the feedback, I have updated the PR with a wording close to this.
The intent is to make clear that undef constructors are not the right way to initialize arrays to missing. This way, other array implementations (such as SentinelArrays.jl) are free to provide faster undef constructors that actually leave data uninitialized (without creating an incompatiblity with specified behavior from Base).
715118e
to
74467d5
Compare
This is a follow-up to #31091. The intent is to make clear that
undef
constructors are not the right way to initialize arrays to missing. This way, other array implementations (such as SentinelArrays.jl) are free to provide fasterundef
constructors that actually leave data uninitialized (without creating an incompatiblity with specified behavior from Base).