Skip to content

Commit abbe045

Browse files
authored
FAQ: Clarify use of PROGRAM_FILE for importable scripts (#50139)
In issue #45852, the proposed `@is_script` macro as a shorthand for `abspath(PROGRAM_FILE) == @__FILE__` was rejected, because it was agreed that the pattern of using the same file as a script and an importable library is questionable, so there is no reason to make it too idiomatic. For this reason, mention in the FAQ that while checking `PROGRAM_FILE` can be used to check if a file is run as a script, it is not recommended to have files double as both scripts and importable libraries.
1 parent 0fb86dd commit abbe045

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

doc/src/manual/faq.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ When a file is run as the main script using `julia file.jl` one might want to ac
9494
functionality like command line argument handling. A way to determine that a file is run in
9595
this fashion is to check if `abspath(PROGRAM_FILE) == @__FILE__` is `true`.
9696

97+
However, it is recommended to not write files that double as a script and as an importable library.
98+
If one needs functionality both available as a library and a script, it is better to write is as a library, then import the functionality into a distinct script.
99+
97100
### [How do I catch CTRL-C in a script?](@id catch-ctrl-c)
98101

99102
Running a Julia script using `julia file.jl` does not throw

0 commit comments

Comments
 (0)