# Submodule initialization

**URL:** <https://discourse.julialang.org/t/submodule-initialization/6325>\
**Category:** General Usage\
**Created:** [October 8, 2017, 2:26pm UTC](https://discourse.julialang.org/t/submodule-initialization/6325 "2017-10-08T14:26:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Belopolsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_belopolsky/32/4882_2.png) [@Alexander\_Belopolsky](https://discourse.julialang.org/u/Alexander_Belopolsky)\
**Post date:** [October 8, 2017, 2:26pm UTC](https://discourse.julialang.org/t/submodule-initialization/6325/1 "2017-10-08T14:26:56Z")

</div>

According to the [“Module-initialization-and-precompilation”](https://docs.julialang.org/en/latest/manual/modules/#Module-initialization-and-precompilation-1) 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:

```julia
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

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

```

Is this expected?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [October 9, 2017, 5:44pm UTC](https://discourse.julialang.org/t/submodule-initialization/6325/2 "2017-10-09T17:44:41Z")

</div>

Great question. This seems to be a consequence of [these lines](https://github.com/JuliaLang/julia/blob/1216e5f60cd2b23e29856b5227399ab0f3abef76/src/toplevel.c#L264-L279), 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?

---

<div class="post-metadata">

**Author:** ![Alexander\_Belopolsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_belopolsky/32/4882_2.png) [@Alexander\_Belopolsky](https://discourse.julialang.org/u/Alexander_Belopolsky)\
**Post date:** [October 9, 2017, 5:57pm UTC](https://discourse.julialang.org/t/submodule-initialization/6325/3 "2017-10-09T17:57:35Z")

</div>

I’ll look into it. Meanwhile I opened [gh-24067](https://github.com/JuliaLang/julia/issues/24067) as a reminder.
