4646

Julia 1.0 UndefVarError - Scope of Variable

Question:

I am moving from Julia 0.7 to 1.0. It seems that Julia's rule for the scope of variables changed from 0.7 to 1.0. For example, I want to run a simple loop like this:

<pre class="lang-julia prettyprint-override">num = 0 for i = 1:5 if i == 3 num = num + 1 end end print(num)

In Julia 0.7 (and in most of other languages), we could expect num = 1 after the loop. However, it will incur UndefVarError: num not defined in Julia 1.0. I know that by using let I can do this

<pre class="lang-julia prettyprint-override">let num = 0 for i = 1:5 if i == 3 num = num + 1 end end print(num) end

It will print out 1. But I do want to get the num = 1 outside the loop and the let block. Some answers suggest putting all code in a let block, but it will incur other problems including UndefVarError while testing line-by-line. Is there any way instead of using let blocking? Thanks!

Answer1:

This is discussed <a href="https://stackoverflow.com/q/52187073/10316987" rel="nofollow">here</a>.

Add global as shown below inside the loop for the num variable.

num = 0 for i = 1:5 if i == 3 global num = num + 1 end end print(num)

Running in the Julia 1.0.0 REPL:

julia> num = 0 0 julia> for i = 1:5 if i == 3 global num = num + 1 end end julia> print(num) 1

<strong>Edit</strong>

For anyone coming here new to Julia, the excellent comment made in the answer below by vasja, should be noted:

<blockquote>

Just remember that inside a function you won't use global, since the scope rules inside a function are as you would expect:

</blockquote>

See that answer for a good example of using a function for the same code without the scoping problem.

Answer2:

Just remember that inside a function you won't use global, since the scope rules inside a function are as you would expect:

function testscope() num = 0 for i = 1:5 if i == 3 num = num + 1 end end return num end julia> t = testscope() 1

The unexpected behaviour is only in REPL. More on this <a href="https://discourse.julialang.org/t/repl-and-for-loops-scope-behavior-change/13514" rel="nofollow">here</a>

Recommend

  • Friendly URL for images
  • Inline div with static width
  • Passing istream into a function
  • git: Aborting commit due to empty commit message (editor: atom)
  • Accessing the request.user object when testing Django
  • making an array of bigInteger of size biginteger in java
  • Android: Saving an image to directory
  • Get data from Firebase in Ionic 2
  • Sapui5: How can I set an initial sort order in smarttable?
  • Is MulticastSocket supported in Android 2.3?
  • How to wrap string in span before and after all newlines in PHP?
  • Excel VBA: Forward ParamArray to Application.Run
  • PhoneGap or Appcelerator Implementation
  • Rails 3 Full_Calendar
  • exception thrown while building the java application using netbeans
  • Angular not getting response when it's a non-200
  • Carrierwave default image doesn't display
  • Google Maps V3 (PHP/MYSQL with custome infobox)
  • Display standard razor/mvc 3 validation messages displayed in another language
  • UIImage to UIColor array of pixel colors
  • Can I switch the 'connected' user within an sql script that is sourced by mysql?
  • Hyperlink to Outlook Attachment
  • Python tk scrollbar becomes inactive once text is outside the screen
  • What's the difference between using RDFS/OWL and XML?
  • Compiling and linking NASM and 64-bit C code together into a bootloader [duplicate]
  • SELECT on JSONField with Django
  • Calling a flash ExternalInterface in swiffyobject
  • Delete a ListItem by clicking the ImageView within the ListItem
  • WiX ManagedBootstrapper SetDownloadSource confusion
  • Circular Left Rotation Algorithm in C#
  • xpath assertion failure with dynamic xpath
  • how to run a different select statement based on condition in Hive SQL
  • Unable to create Access token grant type in wso2 API manager store to test API
  • how do i compare two rows and store the similarities of the two rows in another column
  • Firebase: How to read from external DB?
  • convert json to excel in java
  • Create/delete users from text file using Bash script