Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of variables in Julia

When I ran the Julia code below, there was an error: UndefVarError: globalValue not defined.

I thought that the globalValue is a global variable, but it is not. Thus, if I add the command "global globalValue" inside the for loop, my code will work. So, could anyone please have a look at it let me know what happened? Thanks in advance!

globalValue = 1.0;
tempValue   = 0.1;
for ii = 1:10
    # global globalValue; if I add this command, my code will work
    tempValue = 5.0; ## I have a function to update "tempValue"
    if globalValue < tempValue
        globalValue = tempValue;
    end
end
like image 941
nhavt Avatar asked Aug 20 '18 12:08

nhavt


People also ask

What are variables in Julia?

Variables A variable, in Julia, is a name associated (or bound) to a value. It's useful when you want to store a value (that you obtained after some math, for example) for later use.

What is a scope block in Julia?

Certain constructs in the language introduce scope blocks, which are regions of code that are eligible to be the scope of some set of variables. The scope of a variable cannot be an arbitrary set of source lines; instead, it will always line up with one of these blocks. There are two main types of scopes in Julia, global scope and local scope.

What are the different types of scopes in Julia?

There are two main types of scopes in Julia, global scope and local scope. The latter can be nested. There is also a distinction in Julia between constructs which introduce a "hard scope" and those which only introduce a "soft scope", which affects whether shadowing a global variable by the same name is allowed or not.

Is it possible to have nested variables in Julia?

The latter can be nested. There is also a distinction in Julia between constructs which introduce a "hard scope" and those which only introduce a "soft scope", which affects whether shadowing a global variable by the same name is allowed or not. The constructs introducing scope blocks are:


1 Answers

It seems you are on Julia >= 0.7, where the scoping rules have changed.

Long story short, in a local scope, such as your for-loop, global variables are only inherited for reading but not for writing. There are two ways around it:

  • put an explicit global in front of the assignment (what you figured out yourself)
  • wrap everything in a "global local scope" like let ... end block (globalValue isn't really a global variable anymore)

In your case, the second option would look like

let
globalValue = 1.0;
tempValue   = 0.1;
for ii = 1:10
    tempValue = 5.0;## I have a function to update "tempValue"
    if globalValue < tempValue
        globalValue = tempValue;
    end
end
end

You can find more information here:

  • https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/ (in particular this post by Stefan, who is one of the authors of Julia
  • https://docs.julialang.org/en/stable/manual/variables-and-scoping/#scope-of-variables-1

Although I find this a bit annoying myself, there are good reasons for why the change has been made. Also, on should try to avoid changing globals anyway. Let me quote the manual here (see link above):

Avoiding changing the value of global variables is considered by many to be a programming best-practice. One reason for this is that remotely changing the state of global variables in other modules should be done with care as it makes the local behavior of the program hard to reason about. This is why the scope blocks that introduce local scope require the global keyword to declare the intent to modify a global variable.

like image 68
carstenbauer Avatar answered Oct 21 '22 22:10

carstenbauer