Skip to content

Commit b19c3e7

Browse files
authored
[docs] add a better method for creating an empty array (#4031)
1 parent 34992ae commit b19c3e7

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

docs/src/manual/expressions.md

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,22 @@ This happened because `zeros(AffExpr, 2)` calls `zero(AffExpr)` once to obtain a
393393
zero element, and then creates an appropriately sized array filled with the same
394394
element.
395395

396-
This also happens with broadcasting calls containing a conversion of `0` or `1`:
396+
Note that this behaviour is not JuMP-specific. For example, `zeros(BigInt, 2)`
397+
will create two copies of the same instance of `BigInt`:
398+
```jldoctest
399+
julia> x = zeros(BigInt, 2)
400+
2-element Vector{BigInt}:
401+
0
402+
0
403+
404+
julia> x[1] === x[2]
405+
true
406+
407+
julia> objectid(x[1]) == objectid(x[2])
408+
true
409+
```
410+
411+
This also happens with broadcasting:
397412
```jldoctest
398413
julia> x = Vector{AffExpr}(undef, 2)
399414
2-element Vector{AffExpr}:
@@ -416,14 +431,10 @@ julia> x
416431

417432
The recommended way to create an array of empty expressions is as follows:
418433
```jldoctest
419-
julia> x = Vector{AffExpr}(undef, 2)
434+
julia> x = [zero(AffExpr) for _ in 1:2]
420435
2-element Vector{AffExpr}:
421-
#undef
422-
#undef
423-
424-
julia> for i in eachindex(x)
425-
x[i] = AffExpr(0.0)
426-
end
436+
0
437+
0
427438
428439
julia> add_to_expression!(x[1], 1.1)
429440
1.1

0 commit comments

Comments
 (0)