Type definition works in the REPL, doesn't work in a module

Modules do not inherit variables from their parent scopes. This isn’t really about type definitions at all, but simply the fact that you cannot do:

julia> x = 1
1

julia> module Foo
         @show x
       end
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope at show.jl:641

Instead, you need to use using to bring any names from an outer module into the inner one:

julia> module Outer
         x = 1
         
         module Inner
           using ..Outer: x
           @show x
         end
       end
x = 1
4 Likes