Submodule initialization


#1

According to the “Module-initialization-and-precompilation” section of the manual. “if you define a function __init__() in a module, then Julia will call __init__() immediately after the module is loaded”.

Consider the following code:

module A
    module B
        export b
        const b = Ref{Int}(1)
        __init__() = (b[] = 2; println("B, b[] = ", b[]))
    end
    using A.B
    __init__() = println("A, b[] = ", b[])
    @show b[]
end

I expected B. __init__() to be called right after the module B .. end block is processed and the following A module definitions to see b = 2, but when I run the above, I get

b[] = 1
B, b[] = 2
A, b[] = 2

Is this expected?


#2

Great question. This seems to be a consequence of these lines, which creates a list of modules that need initializing and then processes that list only when you get back up to Main. Seems like it would be worth correcting the documentation? Are you willing to do that?


#3

I’ll look into it. Meanwhile I opened gh-24067 as a reminder.