How to resize a 2D or higher dimensional array?

First. The global in global const is redundant. Just const is sufficient.

Second. Instead of defining a global array variable for the workspace at the beginning of the module, it would be better to define a constructor function for the workspace, like the following simplified style.

module Foo

workspace(m, n) = Matrix{Float64}(undef, m, n)

function f(ws, x, y)
    ...calculation with workspace ws...
end

end

ws = Foo.workspace(100, 100)
z = Foo.f(ws, 1.2, 3.4)

Always use global variables by passing them as arguments to the function. It is better not to use a global variable in a function without passing it as an argument.

If the size of the workspace is determined by the problem and the algorithm, you can create an object alg describing the algorithm from the object prob describing the problem with alg = SomeAlg(prob), and use the constructor SomeAlg to create a workspace of the required size in alg.

cf. Problem-Algorithm-Solver style coding and Game of Life example

3 Likes