Given this code:
function main()
println("hello")
end
if abspath(PROGRAM_FILE) == @__FILE__
main()
end
What does the last part do, and how does it work? Where are those variables defined?
Given this code:
function main()
println("hello")
end
if abspath(PROGRAM_FILE) == @__FILE__
main()
end
What does the last part do, and how does it work? Where are those variables defined?
PROGRAM_FILE
is a global constant defined as the string containing the script name passed to the executing Julia process from the command line (the first element of the argv
in C terms, I think).
@__FILE__
is a macro expanding to the name of the file it is expanded in.
The combination shown is the Julia variant of Python's if __name__ == '__main__'
: when the file is run as a script, PROGRAM_FILE
will contain the script name, equal to the current file, and main
will be called. When the file is included or imported from somewhere else, PROGRAM_FILE
will contain the name of whatever script used the current file, and differ from the current file name.