How to store symbols in a package without being parsed?
There seems to be no way to fool the parsing, so you will either have to store the symbols in a separate file, or use some form of ToExpression
.
What I would suggest is to use a softer version of ToExpression
, and represent the r.h.s. as boxes rather than as a string. Here is what I mean:
BeginPackage["myPackage`"];
load;
boxed;
$context = $Context;
$contextPath = $ContextPath;
Begin["`Private`"];
boxed /: SetDelayed[lhs_, boxed[rhs_]] :=
With[{boxedrhs = MakeBoxes[rhs]},
lhs := Block[{$Context = $context, $ContextPath = $contextPath},
ToExpression[boxedrhs]
]
];
Begin["`Temp`"]
load[] := boxed[{a, b, c}];
End[]
End[]
EndPackage[];
The symbols will still be created during parsing, but they will go into the `Private`Temp`
context. If you want, you can remove them, adding a line like Remove["`Private`Temp`*"]
at the end of the package.
The rhs of load
is then converted to boxes. When load
is called, the symbols get parsed with the same environment ($Context
, $ContextPath
), as if they would've been parsed inside the package normally.