A lot has changed in Julia, and I thought I would update this answer to reflect Julia 1.5 (probably most of the changes were 1.0). While I would expect the modern x[:, :, k] to work, as this is still refered to as a SubArray this actually is copy now when in an expression. Instead you must use view():
x= zeros(2, 2, 2)
function init!(y)
y[:]= ones(size(y))
end
init!(view(x, :, :, 1)) # get reference to original items
This gives you the desired result:
julia> x
2×2×2 Array{Float64,3}:
[:, :, 1] =
1.0 1.0
1.0 1.0
[:, :, 2] =
0.0 0.0
0.0 0.0
There are also helper macros for writing it in a more palatable form,
init!(@view x[:,:,1])
but you run the danger of greedy macro parsing if you have other arguments, such that
otherfunc!(@view x[:,:,1], 10)
would give you an error Invalid use of @view macro: argument must be a reference expression. To get around this, there is the kludge @views which turns all SubArrays into views, or you can wrap the argument in parenthesis.
otherfunc!(@views x[:,:,1], 10)
otherfunc!(@view( x[:,:,1]), 10)
You can find more information on the manipulation of Arrays and Matrices in this presentation:
(Youtube) Arrays: slices and views