# 如何优雅地申明一堆相同大小和类型的数组？

``````const NX, NY, NZ = 64, 64, 64
a1 = Array{Float64}(undef, NX, NY, NZ)
a2 = Array{Float64}(undef, NX, NY, NZ)
a3 = Array{Float64}(undef, NX, NY, NZ)
# ...
``````

Fortran 可以很方便直接定义：

``````real(8), dimension(NX,NY,NZ) :: a1, a2, a3    ! 还有很多。。。
``````

``````a1, a2, a3, a4, a5 = [Array{Float64}(undef, NX, NY, NZ) for i=1:5]
``````

``````using Base.Cartesian
const NX, NY, NZ = 64, 64, 64
@nexprs 3 i -> a_i = Array{Float64}(undef, NX, NY, NZ)
``````

1赞

``````@parameters x1, x2, x3
``````

``````a, b, c, x, y, z = @ntuple 6 i -> Array{Float64}(undef, NX, NY, NZ)
``````

``````macro muldef(def::Expr, vars::Symbol...)
defs = Expr(:block)
for var in vars
push!(defs.args, :(\$var=\$def))
end
esc(defs)
end

#test
const NX, NY, NZ = 64, 64, 64

@muldef Array{Float64}(undef, NX, NY, NZ) a1 a2 a3
``````
``````x, y, z, a, b, c = (Array{Float64}(undef, NX, NY, NZ) for _ in 1:6)
``````

``````struct Foo end
# only one allocation call is needed
as = Array{Foo}(undef, k, nx, ny, nz)

# why would one need this
for i = 1:k
@eval \$(Symbol("a", i)) = view(as, \$i, :, :, :)
end
``````