1
$\begingroup$

Say I create $UserBaseDirectory/Autoload/MyPackage/init.m containing:

BeginPackage["MyPackage`"];
Symbol1 = 1;
Begin["`MyContext`"];
Symbol2 = 2;
End[];
EndPackage[];

When I run Mathematica:

  • Symbol1 resolves to MyPackage`Symbol1 and is colored black, indicating it has a value assigned.
  • MyContext`Symbol2 does not resolve, but MyPackage`MyContext`Symbol2 does. However, there is no autocompletion and the symbol is colored blue (indicating no value assigned) even after evaluation.

Is it possible to configure my package such that MyContext`Symbol2 will resolve and have all the front-end niceties?

$\endgroup$
1
  • $\begingroup$ I believe this question is answered here: (29324) (I am sorry no one directed you to this Q&A sooner.) $\endgroup$
    – Mr.Wizard
    Sep 15, 2014 at 14:18

1 Answer 1

3
+50
$\begingroup$

The standard way to expose a symbol which is defined within a private context is to define a usage string outside the private context, as in

BeginPackage["MyPackage`"];
Symbol1 = 1;
Symbol2::usage = "Symbol2 is a test symbol";
Begin["`MyContext`"];
Symbol2 = 2;
End[];
EndPackage[]; 

Actually, any mention of the symbol inside the package but outside the private context will do the job, but there is a long-standing good practice to use this requirement as an excuse to define a usage string.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.