Store in place results from function returning multiple arrays

If you intend to avoid allocation, this seems impossible because you have already allocated new arrays inside the function. At that point you might as well just destructuringly assign those returned arrays to the variables: fx, fy, fz = f(x,y,z). If you want to do in-place mutation, you need to pass the fx, fy, fz instances into the function, like a function f!(xout, yout, zout, x, y, z) ... nothing end, and use in-place versions of whatever you do in the function.

fx, fy, fz .= f(x,y,z) errors because what you’re really doing is creating a Tuple (fx, fy, fz) and trying to broadcast!-reassign the Tuple’s elements. Even if a Tuple were mutable, the variables fx, fy, and fz aren’t being reassigned. An Array is mutable, so we can see what happens:

julia> x,y,z = (0,0,0) # destructuring assignment to variables x,y,z
(0, 0, 0)

julia> x,y,z
(0, 0, 0)

julia> [x,y,z] .= (1,2,3) # elementwise assignment to array [0,0,0]
3-element Array{Int64,1}:
 1
 2
 3

julia> x,y,z
(0, 0, 0)
1 Like