I’m trying to use PackageCompiler to make my code into an app to share. The compiler is working, but it’s forcing me to modify how I deal with input parameters. The code takes in a number of inputs (including functions) and performs a simulation. Previously the inputs were defined in a namedTuple, which is passed into the solver as an argument.
With the compiled app, I am trying to pass in the name of a file that contains the namedTuple as an argument. Then the app will read the inputs from the file and then call the solver with the inputs. But I can’t get Julia to read a file. I’ve tried include and a macro that copy-and-pastes the file contents into the code (see below)with include since it puts the variable in a global scope or with eval since the name of the file is not known at compile time.
If you have a better method for dealing with input parameters please share!
Here’s a MWE showing what I’m trying to do based on ideas from julia - What exactly does include do? - Stack Overflow and `@def` macro generator broken on master - #3 by ChrisRackauckas
inputs.jl (Inputs File)
p = (
a = 1,
b = [(t) -> t^2]
)
main.jl (reads inputs and runs solver)
module Tester
# Macro to copy/paste file contents
macro include(filename::AbstractString)
path = joinpath(dirname(String(__source__.file)), String(filename))
return esc(Meta.parse("quote; " * read(path, String) * "\n; end").args[1])
end
# Load parameters from file and call solver
function main(filename)
include(filename) # Include fails for function p.b
#@include "inputs.jl" # Macro works - but need to hard code filename
#@include filename # Macro fails with variable filename
# Run solver with input parameters
solver(p)
return nothing
end
function solver(p)
println("Running solver with p = ",p)
println("a = ",p.a) # Should be 1
println("p.b[1] = ",p.b[1])
fun = string_to_funct
println("b[1](3) = ",Base.invokelatest(p.b[1]))(3) # Should be 3^2=9
return nothing
end
main("inputs.jl")
end