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

I feel like I’m missing something basic here. The following code works when pasted in to the REPL:

using DataStructures: MutableLinkedList

module MyModule

   struct MapEntry{T<:Number, M, N}
      IC::Array{T,M}
      FC::Array{T,M}
      Vec::Array{T,N}
   end

   AssocMap = MutableLinkedList{MapEntry}
 end

I can then create variables of type AssocMap, which work as I expect. But when I put that same code in a module definition and include() the module file, I get the following error:

ERROR: LoadError: UndefVarError: MutableLinkedList not defined

pointing to that last line. Which means that the assoc = MutableLinkedList line is interpreted as a type definition when executed in the REPL, but as a variable instantiation when in a file?

The only thing that makes sense to me is that you did not import MutableLinkedList in the context of that module.

I did though. I’ll mod the OP.

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

Yep, that’s it. Thanks!

I should have been clearer. What @rdeits carefully explained was what I intended to mean.