Global variable issue

I found a nice way to check the scopes of functions and variables that have been run from VScode to the terminals REPL. Use varInfo() in the REPL to get a report on what modules have been loaded. Then use .tab completion to track down where the targets scope is.

This is handy if queries like @info myvar return an UndefVarError.

julia> varInfo() 
  name                    size summary
  –––––––––––––––– ––––––––––– –––––––
  Base                         Module
  Core                         Module
  InteractiveUtils 551.066 KiB Module
  Main                         Module
  VXITest          119.475 KiB Module

julia> VXITest.<tab>
VXI
eval
include
ip
julia> VXITest.ip
"192.168.0.40"

julia> VXITest.VXI.<tab>
...
eval                  include
ip    
...

julia> VXITest.VXI.ip
"192.168.0.40"

Aha! What’s this other ip variable-with-the-same-name-as-a-variable-declared-in-the-module-that-included-it? Could be a problem.
I’ve just been translating some code from C# to Julia and the scope thing has caused me to go back to Julia’s manual a lot.

3 Likes

Also try ‘ALT-enter’ which runs the whole thing.
‘CTRL-enter’ will eval the line the cursor is on or the function block its within.

1 Like
    basis[1:s1-1,s+1]=basis[1:s1-1,s]
    basis[s1,s+1]=basis[s1,s]-1
    basis[s1+1,s+1]=N-sum(basis[1:s1,s+1])

so in these codes, ‘basis’ is not reassgined? I think I really need to learn more about the underlying mechanisms of computers or computer languages.

Nope, indexed assignment only mutates the instance assigned to basis, but the instance’s elements are being reassigned. By the way, MATLAB works the same way, though its variables are a bit different from Julia’s in other ways.

1 Like

basis is not reassigned. It is the same array. You are just mutating it.

Note that in the first line basis[1:s1-1,s] this makes a new Matrix of size (s1-1,1). You might want to consider using @view here because we don’t actually need a new Matrix. You just need SubArray referencing part of the original matrix.

2 Likes

Alt-Enter (as mentioned above) or the Base.include seem good enough to me for interactive use since the variables show up in Variable explorer.

Since, it is already possible to evaluate the file with REPL like scoping rules would it make sense to have this as an option for the run button of VS code extension.
It could be like Julia: Execute file in REPL soft scope (use this option for interactive use only) and work if the file did not have other included files i.e. work for only one file scripts. If this were the default option of the Run button it would reduce the number of scope related questions from new users.

No, we shouldn’t encourage people to use REPL rules in files, where it makes sense to have eval/files rules.

This is incredibly hard to prove. I could make an alias to include, even import that alias from a package, so that no include call shows up in the source code. The soft scope rules only exist to let some local scope code be pasted into and work in the global scope or isolated notebook, not inter-includeing files.

1 Like

Something that may be confusing is that = in this code has a different meaning than in most other contexts. When you call a[b] = c, this is not assigning anything, it’s what we call “syntax sugar”, and actually means setindex!(a, b, c), which is a function that mutates a by changing what’s stored at index b to the value c.

4 Likes

What Kevin said is also disoverable. In this case, we can use @which or @code_lowered.

julia> A = zeros(5)
5-element Vector{Float64}:
 0.0
 0.0
 0.0
 0.0
 0.0

julia> @which A[2] = 3
setindex!(A::Array{T}, x, i1::Int64) where T
     @ Base array.jl:1021

julia> function f(A)
           A[1] = 3
       end
f (generic function with 1 method)

julia> @code_lowered f(A)
CodeInfo(
1 ─     Base.setindex!(A, 3, 1)
└──     return 3
)
3 Likes