Type-level variables, i.e. static parameters?

question

#1

I’m writing a parser that has to keep track of many different open files so I have a File type that contains all the relevant information inside of it. Is there any way to do this on instantiation? Say something like this

mutable struct File
    # per-file variables
    offset::Int
    datas
 
    files = Array{File}()

    function File(filename)
        file = new() # open new file and set relevant variables
        push!(files, file)
    end
end

Then I could do something like File.files or equivalent. This is essentially Type-level fields that behave similarly to private static fields in C++


#2

The following seems to accomplish what I want (inspired by https://github.com/JuliaLang/julia/issues/20353)

let
    const files = Dict{String, File}()
    global function add_file(f::File)
        files[f.filepath] = f
    end
    global function get_file_map()
        files
    end
end

#3

What you have above seems to be the best solution if you want something close to C++'s protections.

Alternatively, you can wrap everything in a module, and just not export files. This may make development and debugging easier, but then the variable is modifiable by other functions — you just have to be careful not to do it.