I am used to write Python code, and I find the if __name__ == '__main__' functionality very convenient in the Python open source environment, because it allows to reuse functions written by others (who maybe did not think they would have been useful outside their project) without modifying their code.
I am now dealing with a concrete case, which I submit here for advice. I am contributing to the static-julia project, whose main juliac.jl script calls the julia-config.jl script to define the proper gcc compilation flags. To do this, since julia-config.jl is thought to be directly executed (see function main()) and not to be included by other scripts, juliac.jl has to use the following awkward code to get the flags:
In my mind Python’s if __name__ == '__main__' functionality would be handy in this and similar cases. I would like to ask your opinion and advice on this.
The issue is that I am not the owner of the julia-config.jl script. To do that I would need to ask the maintainer of the project whom julia-config.jl belongs to (in this case Julia itself) to change his code for me. Apart from this specific case (which I do not know what the authors of the Julia would think about it), in general the owner of the project could refuse to change his code for me, at least not in time for my needs, or the project could even be unmaintained.
I think a function similar to include but which only includes functions and similar definitions without actually executing the script would be very useful (it could be named for instalnce includef).
A function like that is impossible. There’s no difference between function definitions and other code. We don’t have a way similar to python for doing this because files are not tied to a module and so executing a script is nothing more than just include it. You don’t have to split the file to make part of it reusable though, you can just have a check for a certain global variable. In either case (python included) the script must be written to support this.
Is the first line part of the “script” or the “functions”? It is not a function, but necessary for the function to work. You will find similar examples with @eval etc all over the Julia codebase.
Yes, but this would work only if the author of julia-config.jl accepts my PR to make my code work, but maybe he would not like to make his code more complex just to make mine work or simply does not care (of course I am taking julia-config.jl just as a specific example but for a more general talk).
The drawback of doing this is that I would duplicate existing code in my project (every time a new version of julia-config.jl is released I would need to copy and paste the relative code into my project).
I agree to some extent, that is why I wrote “functions and similar definitions” (thinking in general about structures, constants, etc). I opened this topic to brainstorm on this, but I am still not convinced that there cannot be better solutions.
For the sake of discussion, what about an includeonly function which allows to specify what exactly to include among available definitions?
For example:
Impossible in the general case, yes. Things might be defined through macros, etc, in ways that are impossible to detect without actually running the code. But it should not be to hard to parse a file and search for expression.head == :type && expression.args[2] == :CustomType, or something like that.
My proposal for the includeonly function was to consider only certain kinds of definitions (e.g. functions, structures, macros, constants, etc), and only those that are at the first level of the script (same level as the main function). So if a function is defined within a macro, the macro itslef should be included. Same if a function is defined within another function, only the external function could be included.
I am not an expert so maybe I cannot see all possible difficulties and consequences, but I think such a functionality would be very handy.
Ok, as I am probably the only one here thinking that an includeonly function would be useful, and I would like to better understand and conform to Julia’s best coding practices, I would like to ask some more information about the following suggestion:
Could you please provide an example or a documentation reference?
Thanks
I think that the julia-config.jl script behaviour should not change when executed, but only when included by another script. Therefore I think this is the best suggestion:
So then what if an environment variable was defined (for instance INCLUDED) which is usually false, and is temporarily set true by the include function itself?
The standard way of doing this would then become:
if !INCLUDED
run_the_script()
end
Even better maybe would be to have function isincluded().
After lots of searching and reading, I think now I have a better understanding on how Julia code should be organized in modules to make it reusable.
I have to say that the “Modules” section of the official Julia documentation (which I had already read before starting this thread) did not help me very much on this, while I found more useful the “Modules and packages” section of the “Introducing Julia” Wikibook.
What however finally opened my eyes are the following two GitHub threads (I add the links here thinking they may be useful for others in future hitting this discussion): https://github.com/JuliaLang/julia/issues/4600 https://github.com/JuliaLang/julia/issues/23619
IMHO the Julia documentation should eventually include more insight and examples on this topic, but I understand that some important details are still under discussion, so I guess we should wait for them to be completely defined and implemented.
Now I feel I am starting to think more Julia than Python, so if you agree in order to make the julia-config.jl functions reusable in juliac.jl I am going to submit a PR with the following edits:
Take all reusable functions out from the julia-config.jl script, and put them in a JuliaConfig.jl script.
In the JuliaConfig.jl script, wrap all the code within a module named JuliaConfig, and export all reusable functions (cflags, ldflags, ldlibs, allflags).
In the julia-config.jl script add the following lines:
include("JuliaConfig.jl")
using .JuliaConfig
Similarly, in the juliac.jl script add the following lines:
include(joinpath("path_to_JuliaConfig", "JuliaConfig.jl"))
using .JuliaConfig
If I understood correctly, this is the current syntax as of Julia v0.6 (please let me know otherwise).
In future, from the two GitHub threads linked above maybe the include statement will not be needed anymore, however in this case it is not clear to me how I would set the "path_to_JuliaConfig". I am going to ask this question there.