# Not possible to "share" top-level variables during precompilation?

**URL:** https://discourse.julialang.org/t/not-possible-to-share-top-level-variables-during-precompilation/92973
**Category:** General Usage
**Created:** [January 14, 2023, 10:55pm UTC](https://discourse.julialang.org/t/not-possible-to-share-top-level-variables-during-precompilation/92973 "2023-01-14T22:55:51Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [January 14, 2023, 10:55pm UTC](https://discourse.julialang.org/t/not-possible-to-share-top-level-variables-during-precompilation/92973/1 "2023-01-14T22:55:51Z")

</div>

I noticed the following behavior I’m trying to understand if its intentional, a bug, or could be changed. It seems obscure but has kind of come up as I’m trying to make Memoization.jl more precompile-friendly. On 1.9-beta2, imagine you have 2 packages:

```julia
module Foo
const caches = IdDict()
set_cache(x) = (@show(objectid(caches)); caches[x] = x)
set_cache("foo")
end

module Bar
using Foo
Foo.set_cache("bar")
end

```

Then you do

```julia
julia> using Bar
[Info: Precompiling Bar [b60c06c0-7e54-11e8-3788-4bd722d65317]
objectid(caches) = 0x52bb3bf00c4b7e9d
objectid(caches) = 0x4de8dd67f96a789a

julia> Bar.Foo.caches
IdDict{Any, Any} with 1 entry:
  "foo" => "foo"

```

So as you can see, during precompilation, apparently, there’s are _two_ different copies of `caches` created. And only the one created inside Foo I suppose persists to the actual session (and contains “foo”).

My question is whether this is intentional? Followup question, would there be a way to have a shared global object that is populated during precompilation from any module? Thanks.

---

<div class="post-metadata">

### Author: ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)
#### Post date: [January 16, 2023, 12:34am UTC](https://discourse.julialang.org/t/not-possible-to-share-top-level-variables-during-precompilation/92973/2 "2023-01-16T00:34:45Z")

</div>

One module owns the binding and the object being pointed to. The idiomatic way is to apply the changes that you want to make during ` __init__ ` in th second module.

---

<div class="post-metadata">

### Author: ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)
#### Post date: [January 16, 2023, 4:34am UTC](https://discourse.julialang.org/t/not-possible-to-share-top-level-variables-during-precompilation/92973/3 "2023-01-16T04:34:50Z")

</div>

That is one way, but ` __init__ ` functions are not particularly idiomatic. The common way to handle this (like Base.Docs) is to embed a variable within the new Module (`Base.Docs.meta()`) that contains the content relevant to that module.
