How does the scope of variables in julia modules work?

Clemens Watzenböck

Running this code

module mod1
export foo

function foo()
    i=0
    for ii in 1:10
        global i+=ii
    end
    i
end

end

mod1.foo()

gives UndefVarError: i not defined.

It seems that the way to do it is by adding a global before the variable i:

module mod2
export bar

function bar()
    global i=0
    for ii in 1:10
        global i+=ii
    end
    i
end

end

mod2.bar()

This gives: 55

Why does the first method not work? As I understand the for introduces a new scope. Therefore I need the global inside the loop. But why do I also need it outside of the loop?

(I am using julia 1.5.0)

François Févotte

The correct way to write this would be:

module mod1
export foo

function foo()
    i=0
    for ii in 1:10
        i+=ii
    end
    i
end

end
julia> mod1.foo()
55

There are 3 scopes introduced here (from outermost to innermost):

  1. global scope of module mod1
  2. local scope of function foo
  3. local scope of for body

The rules for scoping are explained in the manual. In this particular case: when i is referred to in the for body, we first look for a variable named i defined in that same scope. Since none is found, we look for it in the enclosing scope which is the local scope of foo... and find the variable declared using i=0.

If no variable named i had been found in the local foo scope, we'd have had to look into the enclosing scope, which is a global scope. And in this case, you'd have had to put the global keyword to explicitly allow it (because it has performance implications).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I resolve the scope of variables in a julia expression?

From Dev

Does Julia support static variables with function-scope

From Dev

How does JavaScript interpret variables in a scope?

From Dev

How does scope work in Io?

From Dev

how does $scope in controllers work and different ways of declaring controllers?

From Dev

Strange way to pass data between modules in Python: How does it work?

From Dev

How does extract() create variables in the current scope?

From Dev

Angular $scope does not work

From Dev

How does $scope.apply() work exactly in AngularJS?

From Dev

How does local variables actuallly work in lua?

From Dev

Prolog: how does variable instantiating work, what is the scope of local variables

From Dev

How does AngularJS resolves call to variables on $scope in 2 or more controllers?

From Dev

$scope.formName.$setPristine() does not work ; How to reset form in angular?

From Dev

How Does Spring Batch Step Scope Work

From Dev

How does this hoisting work with block scope?

From Dev

How does this hoisting work with block scope?

From Dev

What is the skimlinks scope and how does it work

From Dev

What is the skimlinks scope and how does it work

From Dev

How does scope work in Ruby?

From Dev

Strange way to pass data between modules in Python: How does it work?

From Dev

Angular $scope does not work

From Dev

Accesing script scope variables from modules

From Dev

How does $.ajax success scope work?

From Dev

Prolog: how does variable instantiating work, what is the scope of local variables

From Dev

How does $mdDialog work with promise and $scope?

From Dev

Why does it work with $scope but not with `this`?

From Dev

how does jsp:useBean scope attribute work?

From Dev

Enviroment variables in docker containers - how does it work?

From Dev

Julia: Scope of variables in a nested loop

Related Related

  1. 1

    How can I resolve the scope of variables in a julia expression?

  2. 2

    Does Julia support static variables with function-scope

  3. 3

    How does JavaScript interpret variables in a scope?

  4. 4

    How does scope work in Io?

  5. 5

    how does $scope in controllers work and different ways of declaring controllers?

  6. 6

    Strange way to pass data between modules in Python: How does it work?

  7. 7

    How does extract() create variables in the current scope?

  8. 8

    Angular $scope does not work

  9. 9

    How does $scope.apply() work exactly in AngularJS?

  10. 10

    How does local variables actuallly work in lua?

  11. 11

    Prolog: how does variable instantiating work, what is the scope of local variables

  12. 12

    How does AngularJS resolves call to variables on $scope in 2 or more controllers?

  13. 13

    $scope.formName.$setPristine() does not work ; How to reset form in angular?

  14. 14

    How Does Spring Batch Step Scope Work

  15. 15

    How does this hoisting work with block scope?

  16. 16

    How does this hoisting work with block scope?

  17. 17

    What is the skimlinks scope and how does it work

  18. 18

    What is the skimlinks scope and how does it work

  19. 19

    How does scope work in Ruby?

  20. 20

    Strange way to pass data between modules in Python: How does it work?

  21. 21

    Angular $scope does not work

  22. 22

    Accesing script scope variables from modules

  23. 23

    How does $.ajax success scope work?

  24. 24

    Prolog: how does variable instantiating work, what is the scope of local variables

  25. 25

    How does $mdDialog work with promise and $scope?

  26. 26

    Why does it work with $scope but not with `this`?

  27. 27

    how does jsp:useBean scope attribute work?

  28. 28

    Enviroment variables in docker containers - how does it work?

  29. 29

    Julia: Scope of variables in a nested loop

HotTag

Archive