Hi!
My question is about a good way to include programmatically generated julia file .
The example is the following: I have a pkg X
which generates julia models (functions or DSL-macros) from some external source file (xml-based modeling language) and includes function to simulate a model:
module X
struct MyModel
f1::Function
f2::Function
end
function generate_model_jl_file(path_to_xml_file)
##
return path_model_jl_file
end
function build_model(path_to_xml_file)
path = generate_model_jl_file(path_to_xml_file)
include(path)
return Base.invokelatest(__mymodel)
end
function simulate(m::MyModel)
##
end
end#module
the generated model.jl
file is a function (or macro) that defines a number of functions and wraps it into MyModel
struct with __mymodel
function:
function __mymodel()
## define a number of functions: f1, f2
return MyModel(f1, f2)
end
The reason I wrap include
inside of a function is that I need to hide all technical steps from the enduser (who doesn’t know julia) and allow him to run a single line m=build_model(path_to_xml_file)
The main bad thing about this code is it loads __mymodel
function inside of the X
module. So all functions defined in __mymodel()
are also loaded to X.f1, X.f2
. That is bad in general. But also it makes problematic to simulate model on remote workers (@spawnat 2 simulate(m)
). Is it possible to rewrite the same idea to include
not into X
module but into users Main
?