How to define a const array first and give its length later in a function?

I have a question, in a module,
is it possible that I define a const array first with its length undetermined, and later in a function I set its length?

Like, I do

const A = Array{Float64,1}    however its length is undetermined and need to be set in a function.

The function should be like

function set_A_Length(length::Int64)
  set the length of const A
end

So that if I do set_A_Length(100) I can set A’s length to 100.

Sometimes I need this, because like, I need to read in from a txt file the length, then use function set_A_Length to set the length of A.

I wonder, is the above thing possible?

Thanks in advance!

What is the role of A in between being defined without a length and setting its length?

A array does not do anything in between.
I am a Fortran guy and new to Julia, so I guess am still thinking in a Fortran way.

The size of A may not be determined at the beginning of the module.
I need to use a function to set its size. E.g., I have a txt file in which there is a number 100. I need to read in this 100 first, then I can use this 100 to set the size of A array as 100. However without the txt file, I do not know the size of A array. So the size of A array is actually a variable.

I am curious how Julia people deal with similar situation.

I might be missing something, but why don’t you just create A after you know the size? I vaguely remember from reading some of my advisor’s code during my PhD that in Fortran all variables were usually declared together with their respective types at the start of a script, that’s not necessary in Julia.

2 Likes

I direct answer to your question could be

# create an empty Vector{Float64}
A = Float64[]   # this is the same as A = Vector{Float64}()

# change its size to 100
resize!(A, 100) 

# It's now filled with garbage values, that you must overwrite 

It is possible that you are coming at the problem from the wrong angle. You don’t generally need to ‘declare’ variables in dynamic languages such as Julia, so you could just wait to create A until you know the size you want.

6 Likes

First, @nilshg is right, most of the times, in such situation you just create the array after knowing its size.

However, this is not every situation, sometimes because of some particularity you may need create it without defined length and change it after.

The main problem here is that I think you misunderstand what const means; because const A = Vector{Float64}(); resize!(A, 100) is perfectly valid code. const does not mean that the object first assigned to A becomes immutable, it means the binding of this object to A is constant. In other words, it means that A will always refer to the same Vector object you initially assigned to it (you cannot attribute a new vector to A), but the Vector that is inside A is mutable and can assume any configuration.

3 Likes

Indeed, you are trying to declare variables, that is normal. You can also push values to an array:

julia> x = Float64[]
Float64[]

julia> push!(x,1.0)
1-element Vector{Float64}:
 1.0

julia> push!(x,2.0)
2-element Vector{Float64}:
 1.0
 2.0

This is fine, also in terms of performance, in many situations, and very practical, particularly for reading data from files, when you don’t know the number of data points in advance.

1 Like

Let’s compare it to a wallet.

The global constant const A = Float64[] is similar to a constant, empty wallet.

Money can be stored in the wallet while keeping the wallet itself constant. Putting money into the wallet does not change the wallet itself.

For the const A = Float64[] case, you can also do push!(A, money), etc.

The global variable B = Float64[] is also an empty wallet, but B can later become a different wallet or something other than a wallet.

1 Like