Skip to content

Commit

Permalink
Improve documentation for fill (#41340)
Browse files Browse the repository at this point in the history
Ref #41209
  • Loading branch information
mbauman authored Jul 19, 2021
1 parent c876fba commit e4a6f1d
Showing 1 changed file with 72 additions and 12 deletions.
84 changes: 72 additions & 12 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -431,14 +431,74 @@ to_dim(d::Integer) = d
to_dim(d::OneTo) = last(d)

"""
fill(x, dims::Tuple)
fill(x, dims...)
fill(value, dims::Tuple)
fill(value, dims...)
Create an array filled with the value `x`. For example, `fill(1.0, (5,5))` returns a 5×5
array of floats, with each element initialized to `1.0`.
Create an array of size `dims` with every location set to `value`.
`dims` may be specified as either a tuple or a sequence of arguments. For example,
the common idiom `fill(x)` creates a zero-dimensional array containing the single value `x`.
For example, `fill(1.0, (5,5))` returns a 5×5 array of floats,
with `1.0` in every location of the array.
The dimension lengths `dims` may be specified as either a tuple or a sequence of arguments.
An `N`-length tuple or `N` arguments following the `value` specify an `N`-dimensional
array. Thus, a common idiom for creating a zero-dimensional array with its only location
set to `x` is `fill(x)`.
Every location of the returned array is set to (and is thus [`===`](@ref) to)
the `value` that was passed; this means that if the `value` is itself modified,
all elements of the `fill`ed array will reflect that modification because they're
_still_ that very `value`. This is of no concern with `fill(1.0, (5,5))` as the
`value` `1.0` is immutable and cannot itself be modified, but can be unexpected
with mutable values like — most commonly — arrays. For example, `fill([], 3)`
places _the very same_ empty array in all three locations of the returned vector:
```jldoctest
julia> v = fill([], 3)
3-element Vector{Vector{Any}}:
[]
[]
[]
julia> v[1] === v[2] === v[3]
true
julia> value = v[1]
Any[]
julia> push!(value, 867_5309)
1-element Vector{Any}:
8675309
julia> v
3-element Vector{Vector{Any}}:
[8675309]
[8675309]
[8675309]
```
To create an array of many independent inner arrays, use a [comprehension](@ref man-comprehensions) instead.
This creates a new and distinct array on each iteration of the loop:
```jldoctest
julia> v2 = [[] for _ in 1:3]
3-element Vector{Vector{Any}}:
[]
[]
[]
julia> v2[1] === v2[2] === v2[3]
false
julia> push!(v2[1], 8675309)
1-element Vector{Any}:
8675309
julia> v2
3-element Vector{Vector{Any}}:
[8675309]
[]
[]
```
See also: [`fill!`](@ref), [`zeros`](@ref), [`ones`](@ref), [`similar`](@ref).
Expand All @@ -452,15 +512,15 @@ julia> fill(1.0, (2,3))
julia> fill(42)
0-dimensional Array{Int64, 0}:
42
```
If `x` is an object reference, all elements will refer to the same object:
```jldoctest
julia> A = fill(zeros(2), 2);
julia> A = fill(zeros(2), 2) # sets both elements to the same [0.0, 0.0] vector
2-element Vector{Vector{Float64}}:
[0.0, 0.0]
[0.0, 0.0]
julia> A[1][1] = 42; # modifies both A[1][1] and A[2][1]
julia> A[1][1] = 42; # modifies the filled value to be [42.0, 0.0]
julia> A
julia> A # both A[1] and A[2] are the very same vector
2-element Vector{Vector{Float64}}:
[42.0, 0.0]
[42.0, 0.0]
Expand Down

0 comments on commit e4a6f1d

Please sign in to comment.