I’m writing a Monte Carlo code in Julia that involves several functions that, in the end, have to deal with some variables that are transversal tomall the parts of the code, such as the density, temperature etc. My best bet is that these should be global variables, but if I understand that correctly, using global variables is a bad idea. The way I’m handling that is to define a user type that contains all these variables, as for instnance
then declare a variable of this type, fill it with the right values, and pass it to all functions
glob = global()
glob.dens = 0.02186
This seemsnto work fine and is performance-wise, but due to the use of the user data type, useful operations on whole arrays like
Give an error, stating it can not determine the size of glob.
What would then be the right way to handle that? Actually, what is the best way to deal with global variables in general? I know some will say that the best practice isnto avoid using them, but sometimes I don’t seem to be able to dind alternatives. And I want to avoid passing only the required variables in each case, as sometimes I may need to pass a lot of arguments…
Best regards and thanks,