-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Standardize the entry-point for Julia execution #50974
Changes from all commits
19a3061
e6bf018
61df2d9
1567a0f
edb89c5
cc4108b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,58 @@ $ julia --color=yes -O -- script.jl arg1 arg2.. | |
|
||
See also [Scripting](@ref man-scripting) for more information on writing Julia scripts. | ||
|
||
## The `Main.main` entry point | ||
|
||
At the conclusion of executing a script or expression, `julia` will attempt to execute the function | ||
`Main.main(ARGS)` (if such a function has been defined). This feature is intended to aid in the unification | ||
of compiled and interactive workflows. In compiled workflows, loading the code that defines the `main` | ||
function may be spatially and temporally separated from the invocation. However, for interactive workflows, | ||
the behavior is equivalent to explicitly calling `exit(main(ARGS))` at the end of the evaluated script or | ||
expression. | ||
|
||
!!! compat "Julia 1.11" | ||
The special entry point `Main.main` was added in Julia 1.11. For compatibility with prior julia versions, | ||
add an explicit `VERSION < v"1.11" && exit(main(ARGS))` at the end of your scripts. | ||
|
||
To see this feature in action, consider the following definition, which will execute the print function despite there being no explicit call to `main`: | ||
|
||
``` | ||
$ julia -e 'main(ARGS) = println("Hello World!")' | ||
Hello World! | ||
$ | ||
``` | ||
|
||
Only the `main` binding in the `Main`, module has this special behavior. For example, using `hello` | ||
instead of `main` will result in the `hello` function not executing: | ||
|
||
``` | ||
$ julia -e 'hello(ARGS) = println("Hello World!")' | ||
$ | ||
``` | ||
|
||
The `main` binding may be imported from a package. A hello package defined as | ||
|
||
``` | ||
module Hello | ||
|
||
export main | ||
main(ARGS) = println("Hello from the package!") | ||
|
||
end | ||
``` | ||
|
||
may be used as: | ||
|
||
``` | ||
$ julia -e 'using Hello' | ||
Hello from the package! | ||
$ julia -e 'import Hello' # N.B.: Execution depends on the binding not whether the package is loaded | ||
$ | ||
``` | ||
|
||
However, note that the current best practice recommendation is to not mix application and reusable library | ||
code in the same package. Helper applications may be distributed as separate pacakges or as scripts with | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the catch, but commenting on old (merged and subsequently reverted PRs doesn't really help). If the issue is present in current documentation, please submit a new PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry about that. I checked |
||
separate `main` entry points in a package's `bin` folder. | ||
JeffBezanson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Parallel mode | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this introduces a regression in a failure to run a repl if REPL_MODULE_REF is not defined.