One of the performance tips of Julia is not using global variables. I have a problem in which
I need to pass a lot of data to a function, but the function needs to be called in a specific way,
and the data must be passed “under the hood” to the function. In Fortran I used modules for that
purpose (or commons, before that), and the variables of the data were visible only for the functions
that explicitly appealed to them (i. e. use Data). I cannot find, or understand, a satisfactory alternative in Julia. For example:
struct Data
a :: Int64
end
data = Data(2)
function foo(x :: Int64)
return data.a * x
end
x = 1
foo(x)
First, it seems that “data” is a global variable. I am not sure if passing data like this results in a performance loss. If so, how to improve that, given that I cannot change the syntax of the function call? Is the use of data as in the example above worse than the use of modules in Fortran, or is it the same sort of thing?
Second, it bothers me that I do not need to explicitly state inside the function that I will be using the
data of the global variable “data”. Should I add a “global data” to make that explicitly as good practice?
What confuses me here is that declaring the variable as global changes how the function sees the data, as a modification of its values results, or not, in an undefined error:
b=1
function foo()
global b
b = b + 1
end
foo()
function foo_error()
b = b + 1
end
foo_error() # ERROR: b not defined
function foo2()
return b + 1
end
foo2() # But this works
Additionally, can I change the name of the variable in the local scope? (That is, something like “using data as mydata”).