27 mai 2024
Julia : passer un argument avec include(‘file.jl’)
Une méthode intéressante consiste à utiliser isdefined
pour vérifier la présence d’une source différente de paramètres avant d’utiliser ARGS
dans le fichier que l’on souhaite inclue. Par exemple :
main.jl
newARGS = String["adios","amigos"]
include("file.jl")
file.jl
localARGS = isdefined(:newARGS) ? newARGS : ARGS
@show localARGS
Ce qui donnerait :
> julia file.jl hello world
localARGS = String["hello","world"]
> julia main.jl
localARGS = String["adios","amigos"]
Reassigning ARGS to make file.jl think it received arguments works, but leads to a warning (because it overwrites Base.ARGS). A better methods perhaps is to use isdefined to check for a different source of parameters before using ARGS in file.jl. This also allows communicating deeper through several levels of inclusion.