# I cannot import \`global variable::Type\`

**URL:** <https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 3, 2024, 11:01am UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468 "2024-04-03T11:01:21Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [April 3, 2024, 11:01am UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/1 "2024-04-03T11:01:21Z")

</div>

Hi there,

I am using submodules and am surprised that I cannot import a globally defined config value which is not yet initialized.

While julia has a special syntax for such undefined globals

```julia
global variable::Type

```

I am unable import this undefined variable inside a submodule `using TopLevelProject: variable`. I get an error

```julia
ERROR: LoadError: UndefVarError: `variable` not defined

```

I guess the workaround arround is to use a const Ref, still I am very surprised that such typed globals cannot replace const Ref.

---

<div class="post-metadata">

**Author:** ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)\
**Post date:** [April 3, 2024, 6:20pm UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/2 "2024-04-03T18:20:04Z")

</div>

I think you should get the same error within the defining module if you haven’t assigned a value to it yet (like regularly trying to access the varable).  
To avoid this, you check with `@isdefined` first or assign a sentinel value initially.

Another way would be to define

```julia
variable() = TopLevelProject.variable

```

which should also avoid the issue.

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [April 5, 2024, 2:45pm UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/3 "2024-04-05T14:45:05Z")

</div>

I am only using this inside Functions, so there is no need to really access the variable in precompile/macroexpand time.

---

<div class="post-metadata">

**Author:** ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)\
**Post date:** [April 5, 2024, 6:07pm UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/4 "2024-04-05T18:07:05Z")

</div>

I am not sure I follow - do my suggestions solve your problem?

I think the misconception may arise from what a variable in Julia is, conceptually:

```julia
julia> global g

julia> @isdefined g
false

julia> g = 2
2

julia> @isdefined g
true

```

In Julia, variables are bindings and to create a binding, there needs to be an object to create a binding to.

Whereas, for example in C, variables can be seen as boxes, so if you do declare a variable

```java
int i;

```

then you already get the box - you just don’t know what’s in it, meaning `i` has the value of whatever bits were at this place in memory before.

To achieve the behaviour you want in Julia, you could:

1. Just use a Ref - like you suggested.

2. Depending on what `Type` actually is, choose a “placeholder” value that you can assign immediately.

3. Only refer to the variable once it actually has a value (which you can check via `@isdefined`).

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [April 5, 2024, 8:09pm UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/5 "2024-04-05T20:09:02Z")

</div>

I think a `const Ref` is probably the way you want to go if you cannot initialize the global for some reason. With a `Ref` you can at least have an `#undef`.

What kind of `Type` were you considering this for?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 5, 2024, 8:27pm UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/6 "2024-04-05T20:27:22Z")

</div>

I’m a little confused: what would you do **after** you’ve imported the undefined global? The only thing you _could theoretically do_ is assign to it. But Julia doesn’t let you do that, either:

```Julia-repl
julia> module M
           global var = 1
           export var
       end
Main.M

julia> using .M: var

julia> var = 2
ERROR: cannot assign a value to imported variable Main.var

```

You’ve gotta explicitly fully-qualify the thing:

```Julia-repl
julia> M.var = 2
2

```

And at that point, it _also_ works if it’s not defined!

```Julia-repl
julia> module M2
           global var2
           export var2
       end
Main.M2

julia> using .M2

julia> var2
ERROR: UndefVarError: `var2` not defined

julia> M2.var2 = 3
3

julia> var2
3

```

---

<div class="post-metadata">

**Author:** ![mnemnion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mnemnion/32/206596_2.png) [@mnemnion](https://discourse.julialang.org/u/mnemnion)\
**Post date:** [April 5, 2024, 9:05pm UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/7 "2024-04-05T21:05:42Z")

</div>

> [@mbauman](#):
>
> I’m a little confused: what would you do **after** you’ve imported the undefined global?

I believe the idea here is that the imported variable is used inside functions, which will only be called once the variable is assigned a value.

As you’ve pointed out, one solution would be to not import the variable, and use the module-qualified name for it in the function. A Ref is another viable option here.

The best one is probably to restructure things so that this isn’t necessary. It’s usually possible to do that.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 5, 2024, 9:53pm UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/8 "2024-04-05T21:53:09Z")

</div>

> [@mnemnion](#):
>
> As you’ve pointed out, one solution would be to not import the variable, and use the module-qualified name for it in the function.

Or do `foo = Foo.foo` in the function, so that you only have to qualify it once. (Essentially a “local import”)

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [April 9, 2024, 4:08pm UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/9 "2024-04-09T16:08:45Z")

</div>

While often you don’t need to initialize variables on runtime, often you do need to do so.

In my case, which also answers your question @mkitti, I am using a global Configurations.jl `@option` struct to gather all my variables. Concretely there are some secrets which I need to initialize. I don’t want to write the secrets into the code, so this a classical example where you need runtime initialization of the concrete Configuration object.

Thank you for the workarounds! It is good to know that referencing it via the module name actually works.

For my intuition it is a bug that referencing it directly does not work.  
Does someone know about Julia’s variable importing system whether this should work?

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [June 27, 2024, 10:07am UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/10 "2024-06-27T10:07:43Z")

</div>

I created an issue for this [uninitialized typed global can only be imported qualified and not directly · Issue #54954 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/54954)

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [June 27, 2024, 10:20am UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/11 "2024-06-27T10:20:45Z")

</div>

After all this discussion I still don’t understand what it is you’re trying to do, why do you want a global variable, and why are the suggested solutions not good enough for you. Please provide a minimal working example, then we can help you improve it.

BTW, yet another potential solution to look into could be ScopedValues.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [June 27, 2024, 10:22am UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/12 "2024-06-27T10:22:36Z")

</div>

> [@schlichtanders](#):
>
> Concretely there are some secrets which I need to initialize. I don’t want to write the secrets into the code, so this a classical example where you need runtime initialization of the concrete Configuration object.

Yeah, this seems like a use case for a scoped value.

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [June 27, 2024, 12:12pm UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/13 "2024-06-27T12:12:24Z")

</div>

Yeah, ScopedValues look nice too.

I guess my wish is to somehow get rid of the brackets `[]` which I need to use when using `Ref`. My current workaround is to define a custom `RefType` which actually forwards dot access. It is just because the typed unitialized global variable look like they can do exactly this, but they don’t do it yet. Hence my mind thinks again and again “come on, this workaround shouldn’t be needed as we have such globals”.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [June 29, 2024, 6:40am UTC](https://discourse.julialang.org/t/i-cannot-import-global-variable-type/112468/14 "2024-06-29T06:40:28Z")

</div>

Resolved already!
