I’m coming from languages like C, python and mathematica (I only really use mathematica for some data handling and scripting)
I have quite a few things I’m struggling with to be honest.
The only way I know how to run this code is typing “julia simulation.jl {args}” into cmd. I haven’t really been able to get this to accept arguments in REPL. I’ve searched online and there seems to be some hacky ways of redefining the ARGS objects before you run it to get it to accept them, but I can’t see how this is even close to the intended way.
I’ve been doing everything from VSCode, but I need to share this file with other people so that they can run and test it (this is an assignment so I need the professor to be able to run it without much hassle from his side).
I’ve seen many people recommend wrapping everything in functions, and I think I’m doing that but I can never be too sure.
I’m just at a loss on how to manage all this. Learning about functions online is an alright experience as long as it’s self-contained. But I’m finding that learning about anything on a broader (or “meta”) scope is very difficult and there’s not many sources that actually give a good overview in my opinion
Regarding your args issue, I recommend the following structure.
function main(args::Vector{String} = ARGS)
nargs = length(args)
arg1 = 1
arg2 = 2
if length(args) > 0
arg1 =parse(Int, args[1])
end
if length(args) > 1
arg2 = parse(Int, args[2))
end
simulation(arg1, arg2)
end
function simulation(arg1, arg2,)
# your actual simulation
end
if abspath(PROGRAM_FILE) == @__FILE__
main()
end
This allows you to run the file from the command line or load the file via include. You can then either invoke main(String["1", "2"]) or simulation(1,2).
Usually in Julia you would share your simulation as a package and ask the person you share it with to install or activate it then run some function defined in your package.
julia> using MyAwesomePackage
julia> runSimulation()
You can also define a function named __init__() in your package that will run as part of the using step. I tend to print some user instructions with it.