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 documentation for ones and zeros to clarify that they return arrays filled with the same object (similar to fill) #55912

Open
sadish-d opened this issue Sep 28, 2024 · 0 comments
Labels
arrays [a, r, r, a, y, s] docs This change adds or pertains to documentation

Comments

@sadish-d
Copy link

The documentation of fill clarifies that the returned object is filled with the same object, which is important when the element is mutable. ones and zeros also work the same way, but the documentation is not clear on this (for Julia version 1.10.3). Example:

struct S
    x::Array{Bool, 0}
    S(x) = new(fill(x))
end
Base.zero(::Type{S}) = S(0)
Base.zero(::S) = Base.zero(S)
Base.one(::Type{S}) = S(1)
Base.one(::S) = Base.one(S)
_zeros = zeros(S, 2)
_ones = ones(S, 2)
_zeros[1].x[] = 1
_ones[1].x[] = 0
@assert _zeros[2].x[] == _zeros[1].x[] == 1
@assert _ones[2].x[] == _ones[1].x[] == 0

Here's a draft phrasing to be added to the documentation of zeros and ones, modified from fill's documentation.

Every location of the returned array is set to (and is thus === to) the same value; this means that if the value is itself modified, all elements of the filled array will reflect that modification because they're still that very value. This is of concern when the values are mutable.

@nsajko nsajko added docs This change adds or pertains to documentation arrays [a, r, r, a, y, s] labels Sep 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

2 participants