# How to declare a global variable without initializing it?

**URL:** <https://discourse.julialang.org/t/how-to-declare-a-global-variable-without-initializing-it/105225>\
**Category:** VS Code\
**Tags:** warning\
**Created:** [October 20, 2023, 10:33am UTC](https://discourse.julialang.org/t/how-to-declare-a-global-variable-without-initializing-it/105225 "2023-10-20T10:33:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [October 20, 2023, 10:33am UTC](https://discourse.julialang.org/t/how-to-declare-a-global-variable-without-initializing-it/105225/1 "2023-10-20T10:33:41Z")

</div>

Say I declare the following outside all functions, VSCode complains that exportfile is missing reference, on that line and on all lines referencing the symbol.  
global exportfile::IOStream

But if I initialize it there with  
global exportfile::IOStream = open(“junk”)  
then things are fine.

But I don’t want to initialize it there because, the file name is not available yet.

What can I do?

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [October 20, 2023, 10:54am UTC](https://discourse.julialang.org/t/how-to-declare-a-global-variable-without-initializing-it/105225/2 "2023-10-20T10:54:15Z")

</div>

I will not question why you necessary need to have this as a global (just for the record, I think there might be better ways to do this if you shift a few things around in your code).

However, the following works well for me:

```julia
module MyModule

global X::IOStream
export X

end

using .MyModule

function setX!()
    MyModule.X = open("junk.txt")
end

setX!()

@info X

```

Also, you say that `VSCode` complains - what do you mean by this? I cannot reproduce your issue at any level: both VSCode (Julia extension) and Julia side of things are looking good.

The only thing I can imagine goes wrong is that you actually try to use your variable in some other place before actually initializing it.

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [October 20, 2023, 11:55am UTC](https://discourse.julialang.org/t/how-to-declare-a-global-variable-without-initializing-it/105225/3 "2023-10-20T11:55:21Z")

</div>

To further from your suggestion, my module is more like this:

```julia
__precompile__ (true)

module MyModule

export setX

CZ = Array{String}(undef, 0)

global ffile::IOStream

function setX!()
    global ffile = open("junk.txt")
end

function getX()
    println(ffile, "testing")
    ffile
end

end

```

And indeed, there is no complaint from VSCode.

But my real module, just like this one albeit longer and with more functions, gets complaints. The complaints about missing reference are on these lines:

global ffile::IOStream  
println(ffile, “testing”)

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [October 20, 2023, 12:01pm UTC](https://discourse.julialang.org/t/how-to-declare-a-global-variable-without-initializing-it/105225/4 "2023-10-20T12:01:01Z")

</div>

The following works well for me:

```julia
__precompile__ (true)

module MyModule

export setX!

CZ = Array{String}(undef, 0)

global ffile::IOStream

function setX!()
    global ffile = open("junk.txt", "w")
end

function getX()
    println(ffile, "testing")
    ffile
end

end

using .MyModule
setX!()
MyModule.getX()

```

You must ensure you are not calling `getX` somewhere in your code before `setX!`.

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [October 20, 2023, 12:03pm UTC](https://discourse.julialang.org/t/how-to-declare-a-global-variable-without-initializing-it/105225/5 "2023-10-20T12:03:14Z")

</div>

yes, that’s what I said, this demo code works well for me. I don’t know why my real module like this gets complaints.

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [October 20, 2023, 12:06pm UTC](https://discourse.julialang.org/t/how-to-declare-a-global-variable-without-initializing-it/105225/6 "2023-10-20T12:06:03Z")

</div>

I understand.

Unfortunately, there is nothing much to do without knowing more about the `real` thing.

The _how to (properly) declare a global variable without initializing it_ issue you mentioned doesn’t seem to be the culprit at all. So, I think we are left with trying to find out where/why you try to use the uninitialized variable.

Also - I want to ensure we are on the same page - because you invoked the complaint from VSCode again (this time, you said VSCode doesn’t complain).

Do you mean the code compiles and works well, and you only have an issue with why VSCode Julia extensions show some warning?

Can you clarify the above?
