In particular, a friendly, idiomatic, and performant way to go about it would be to return a named tuple:
function do_stuff2(x)
a = x^2
b = a*cos(x)
c = b*exp(x) + a
return (; a, b, c)
end
makes it easy for the caller to extract any variables they want. e.g. if the caller wants a
and c
, they can do:
julia> (; a, c) = do_stuff2(3.7);
julia> a
13.690000000000001
julia> c
-455.92299991101584
In general, I would say that you are fighting with the language by using Base.@locals
and arrays of symbols here. You can get something that works, but it will be slow (e.g. type-unstable and allocating), fragile, and hard for other Julia programmers to understand. Better to learn how to write idiomatic code, use appropriate data structures, and think carefully about the useful inputs/outputs of your functions (your API) rather than trying to give the caller blanket access to all local variables.
Don’t reach for “metaprogramming” (e.g. generated lists of variable names, code introspection, passing code as strings) as your first resort.