If I’m developing a package where file1.jl
contains a struct called struct1
and file2.jl
has a struct called struct2
as well as a function that took those two structs as input, what would be the best way to organize this?
Currently in the main module for the package I just include the first file before the second. Is that confusing? Should I move include("file1.jl")
to the second file? Am I thinking too hard about this? From an organizational standpoint it makes sense to have the structs in separate files.
Also sorry if this is like blatantly obvious in the documentation. I still new to package development in general.
The way you’re doing it now seems fine to me.
I don’t think it’s confusing to include file1.jl
and file2.jl
from the main top-level source file. It does make sense to have the structs and associated functions/methods grouped in separate files. In that case, the convention is to name the source files after the struct names (e.g. something like my_struct.jl
for a struct named MyStruct
).
As for the function that works on both your types, you could leave it in file2.jl
; this is especially appropriate if there is a dissymmetry in the relationships between your function and both files, and there is a reason to “favor” the function definition being more closely tied to struct2
than struct1
. But you can also move the function to a third feature.jl
file, which would be more symmetric. Alternately, you could move it to the main, top-level source file, right after both includes.
4 Likes
Thank you for the reply. Moving the function to a third file is actually a pretty good idea for this case.