Re-posted from: https://bkamins.github.io/julialang/2024/07/05/main.html
Introduction
This is my last blog post with the previews of an upcoming Julia 1.11 release.
The functionality I want to cover today is an option of defining an entry point to the Julia script.
The code was tested under Julia 1.11 RC1.
A traditional Julia script
Traditionally when writing a Julia script you assumed that when you run a julia some_script.jl
command.
In this case Julia sequentially executes the contents of the some_script.jl
file and terminates.
When I was writing Julia code that was meant to be executed in this way my typical approach was to always encapsulate all executed code in functions.
In this way we can avoid many problems that are introduced by writing code that is executed in global scope, including some of the common issues:
- scope of variables (no need to think about the
global
keyword); - performance (code inside functions is compiled, thus fast);
- an accidental use of the same name for different objects in global scope spaghetti code (I think everyone has been bit by this issue);
- pollution of RAM memory (large objects that have bindings in global scope are kept alive and it is easy to forget to unbind them to alow garbage collection).
Therefore a typical structure of my code was:
...
some definitions of data structures and code inside functions
...
function main(ARGS)
...
the operations I want to have executed by the script
...
end
main(ARGS)
This is a style that is natural for programmers used to such languages as e.g. C, where the main
function is an entry point.
Script under Julia 1.11
Julia 1.11 adds an option to mark the main
function as an entry point. It makes sure that main(ARGS)
gets called after execution of the script.
It is quite easy to mark the main
function as an entry point. It is enough to just replace main(ARGS)
with (@main)(ARGS)
in my example above.
Thus, starting from Julia 1.11 I can write my scripts as:
...
some definitions of data structures and code inside functions
...
function (@main)(ARGS)
...
the operations I want to have executed by the script
...
end
This seemingly small change is in my opinion significant as it standardizes the way Julia scripts are written.
And such standardization is a good feature improving code readability and maintainability.
Additionally, this feature helps in unification of interactive and compiled workflows of using Julia.
Let me show a minimal working example of writing a script using the @main
macro:
$ julia -e "using InteractiveUtils; (@main)(args) = versioninfo()"
Julia Version 1.11.0-rc1
Commit 3a35aec36d (2024-06-25 10:23 UTC)
Build Info:
Official https://julialang.org/ release
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: 12 × 12th Gen Intel(R) Core(TM) i7-1250U
WORD_SIZE: 64
LLVM: libLLVM-16.0.6 (ORCJIT, alderlake)
Threads: 1 default, 0 interactive, 1 GC (on 12 virtual cores)
$
In this example we invoke the versioninfo
function inside the main(args)
function defined using the @main
macro.
Note that we did not have to explicitly call the main
function in the code. It was invoked automatically because it has
been created using the @main
macro.
Conclusions
Now I hope you know what @main
macro does and how to use it in Julia 1.11. Enjoy scripting with Julia!