How to define local or global variables?

So far the most frustrating part of using Julia has been the below annoying error message:

Assignment to `IDOutput` in soft scope is ambiguous because a global variable by the same name exists: `IDOutput` will be treated as a new local. Disambiguate by using `local IDOutput` to suppress this warning or `global IDOutput` to assign to the existing global variable.

My code:
IDOutput = [];

If I have only a few input parameters, I would wrap everything into a function. But sometimes, I have dozens and dozens of parameters. What is the most grace way of fixing this issue?

Is it even true that “a global variable by the same name exists”? No matter how I change the name to something unique, the system would simply throw out the same error.

You can just put let at the beginning of your script and end at the end, and voila: everything is a local variable. (But using and import and include statements must go outside the let block.)

But in general the way to deal with passing lots of parameters is to define one or more struct types encapsulating them. (Or use a named tuple, which is essentially a struct without a name.) Coming up with good data structures and abstractions is hard, but the payoffs in long run (for maintainability, flexibility, composability, and performance) are huge.

4 Likes

Many thanks for sharing the below trick. It works!

let
...
end