Functions with independent (global) scopes

Regarding a function named main, see (on hold): If you have a function called `main`, you may need to tweak it

You may be interested in the contextual module REPL introduced in Julia 1.9. This lets you change the module from Main to an arbitrary module, effectively changing your global scope.

https://docs.julialang.org/en/v1/stdlib/REPL/#Changing-the-contextual-module-which-is-active-at-the-REPL

Julia modules are a top level encapsulation. I believe you can effectively accomplish your goals by changing the module context.

Here is a verbose demonstration.

julia> x = 5
5

julia> varinfo()
  name                    size summary
  –––––––––––––––– ––––––––––– –––––––
  Base                         Module
  Core                         Module
  InteractiveUtils 529.576 KiB Module
  Main                         Module
  ans                  8 bytes Int64                      
  x                    8 bytes Int64
                                                        
julia> using REPL

julia> module Foo end
Main.Foo                                                
julia> REPL.activate(Foo)
                                                        
(Main.Foo) julia> varinfo()
  name size summary
  –––– –––– –––––––
  Foo       Module

(Main.Foo) julia> x
ERROR: UndefVarError: `x` not defined

(Main.Foo) julia> x = 6
6

(Main.Foo) julia> x
6

(Main.Foo) julia> using ..REPL

(Main.Foo) julia> REPL.activate()

julia> x
5

julia> Foo.x
6
6 Likes