julia: recommended way to `include` files: list of includes in top-level file or nested includes?
Asked Answered
A

1

7

What is the recommended way to include all the files for your project?

I have seen many examples that use a similar structure: an ordered list of include statements in a single top-level file (the file that defines a Module, or the "main" file in an application).

This also seemed to be the conclusion in the following thread: https://discourse.julialang.org/t/best-practices-for-structuring-larger-projects/2652?u=nhdaly


However, Lint.jl seems to be very unhappy with this sort of project structure, complaining about the persistent use of undeclared symbols.

Consider the following example:

# --- src/lib1.jl ---
struct MyStruct
    x::Int32
end

# --- src/lib2.jl ---
foo() = MyStruct(3)    # Lint.jl says: E321 MyStruct: use of undeclared symbol

# --- src/MyPackage.jl ---
module MyPackage

include("lib1.jl")
include("lib2.jl")

foo()

end

Of course, this code will work correctly, because MyStruct will be available before lib2.jl is compiled. But Lint.jl cannot know that. Even when using the Atom-integrated linter, it shows the same error.

So is it preferable to have lib2.jl include("lib1.jl") instead of all the includes in the top file? Or is it best to put them in both places, as you might for C++ headers? I haven't seen a definitive recommendation for this anywhere, and I'd love if someone could provide one! :)


EDIT: changed file names to reflect a more typical julia directory structure.

Albers answered 29/4, 2018 at 5:50 Comment(3)
Is there a strong reason for you not to use modules?Continuo
This seems like a bug in Lint.jl?Clausius
Huh, thanks. I've filed github.com/tonyhffong/Lint.jl/issues/248.Albers
H
3

I usually do a:

PkgDev.generate("MyPackage", "MIT")

This generates the whole folder structure. I put my Julia(.jl) files in the src folder and use 'using' to import functions in other Julia files in the same folder.

Hedva answered 7/8, 2018 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.