Usage of parentmodule()

#1

I’m having some trouble with the new parentmodule() function in Julia 1.0. According to the Julia 0.7.0 release notes module_parent() is depreciated in favor of parentmodule(), however parentmodule() in Julia 1.0 returns differently than module_parent() did in Julia 0.6.
Some example code:

#julia 1.0
module ModA
   global __parent = nothing
   function __init__()
      global __parent = parentmodule(ModA)
   end
end

using ModA
ModA.__parent

This returns ModA.__parent = ModA

#julia 0.6
module ModA
   global __parent = nothing
   function __init__()
      global __parent = module_parent(ModA)
   end
end

using ModA
ModA.__parent

This returns ModA.__parent = Main

Is this how moduleparent() is supposed to work? If so, how do I get the same functionality as the Julia 0.6 code?

0 Likes

#2

name the initialization function __init__ not init, then it works as you expect

module Module
    the_parent = nothing  # no need to declare this global
    function __init__()
        global the_parent  = parentmodule(Module)
    end
end

julia> using .Module # notice the `.` (Module is defined in the REPL)

julia> Module.the_parent
Main
1 Like

#3

Hi Jeffrey
Thank you for the reply, I edited my original post to put the code in a code block, sorry, I missed that. The problem comes in when you call the module from the load path like so:

push!(LOAD_PATH, "path to module")

using ModA
ModA.__parent
0 Likes

#4

In an effort to make Main less special in v1.0, many modules now are their own parent, instead of requiring them all to live in the Main namespace.

1 Like

#5

Hi Jameson.
Thank you for the reply, so the code work as intended and the module parent actually changed, that’s good to know.

0 Likes