How to declare a global variable without initializing it?

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?

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:

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.

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

__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”)

The following works well for me:

__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!.

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.

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?