Open
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