The documentation is accurate: zip
doesn’t allocate an array, it returns an iterator (without allocation). Of course an array is iterable so you can use the (; A...)
syntax with an array, it’s just not what happens here with zip
.
When I have an iterator of name-value pairs, my preference is to use the NamedTuple(iterator)
constructor:
keys = (:a, :b, :c); values = (1, 2, 3)
it = zip(keys, values)
julia> NamedTuple(it)
(a = 1, b = 2, c = 3)
But the (; ...)
syntax is more flexible, it’s super useful in more complex cases:
keys = (:a, :b, :c); values = (1, 2, 3)
it = zip(keys, values)
A = [:c => 300, :d => 400]
mykey = Symbol("e", rand(3:5))
myval = 5000
other = (e="X", f="Y", g="Z")
julia> (; it..., b=20, A..., mykey => myval, other.f)
(a = 1, b = 20, c = 300, d = 400, e4 = 5000, f = "Y")
This takes the key-values from it
, then overrides the b
value, then takes the key-values from A
(overriding c
and adding d
), then adds an element with a dynamically generated name, and finally adds an element from other
, reusing the name.