I have a package file myPackage.m
with the following content:
BeginPackage["myPackage`"];
load[] := {a, b, c};
EndPackage[];
When Needs
is called, and the file is parsed, all the symbols in the package immediately appear in the myPackage`
namespace, regardless of whether load
was called or not:
Needs["myPackage`"];
Names["myPackage`*"]
{"a", "b", "c", "load"}
Is there an elegant, domestic way to only parse symbols into the given context (myPackage`
in this case) when the function that includes them is called and not at parse-time? No amount of HoldComplete
, Unevaluated
, etc. works. I rather avoid storing the rhs
(or the symbols directly) of load
as a string, ToExpression
-ing it only at runtime.
The only option I can think of is to have load[]:=Get["load.m"]
and then store the actual definition in load.m
, but this is cumbersome if one has many different functions, like load1
, load2
, etc., because then each definition has to be stored in a different file. Is there a clever way of playing with contexts to overcome this challange?
The reason behind is that I have to store a large amount of equation-systems (say hundreds), each with a specific name and variables, and I want them (and their variables) to be available only if they are explicitly called for by name. Thus if I call load["eq12"]
, I expect that the symbols of "eq12"
(but only its symbols) appear in the namespace. If then I call load["eq35"]
, its symbols are added to the namespace too. Also, I don't want the equation variables to appear as myPackage`private`x
, so I try not to define the equations in a private`
subcontext.
One can use the formal symbols of the System`
context (like \[FormalX]
, which are already loaded to the context), but then the set of variables is limited to be single character-variables.
myPackage`eq12`a
? This way you could add/remove those additional contexts, to/from$ContextPath
, at will. $\endgroup$BeginPackage[]
-EndPackage[]
or each symbol should be written with its full name. Both are pain in the ass if not done programmatically. Would you perhaps care to develop an answer based on the idea? $\endgroup$