Skip to content

Add documentation for ones and zeros to clarify that they return arrays filled with the same object (similar to fill) #55912

Open
@sadish-d

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]docsThis change adds or pertains to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions