4
$\begingroup$

I am writing a package like this:

BeginPackage["HNotation2`"];

Begin["`Private`"];

HTransR=3;

End[];

EndPackage[];

When I load the package, it seems that the symbol HTransR is not defined. Is there something wrong with the way that I'm assigning a value to the symbol in the package?

$\endgroup$
  • $\begingroup$ First, it should be Begin["Private`"];. Afterwards, you will find HTransR in the context "HNotation2`Private`". Unless you let appear the symbol HTransR already before Begin["Private`"];, you have to refer to it by HNotation2`Private`HTransR from outside that package. $\endgroup$ – Henrik Schumacher Feb 4 '19 at 17:58
  • $\begingroup$ @HenrikSchumacher - I am confused by the first sentence of your comment. $\endgroup$ – Jason B. Feb 4 '19 at 18:04
  • 2
    $\begingroup$ but it should be Begin["`Private`"] $\endgroup$ – Jason B. Feb 4 '19 at 18:05
  • $\begingroup$ @JasonB. Oops. Sorry. I did not know about that possibility (good to know!). I tend to use the full context path (Begin["HNotation2 `Private`"];), but of course that is a bit redundant and error prone. Once again, sorry guys for the confusion caused by me. $\endgroup$ – Henrik Schumacher Feb 4 '19 at 18:29
4
$\begingroup$

With respect, I believe Begin["`Private`"] is acceptable, and perhaps preferable. Begin["Private`"] establishes a context "Private`". This is fine, but if two packages do that they will share that context. Begin["`Private`"] establishes context HNotation2`Private`. This will be private to this package only.

The reason HTransR appears to be undefined is that it is defined within context HNotation2`Private`, which is not on the $ContextPath -- it can be accessed by its full name as HNotation2`Private`HTranR.

If you wish it to be accessible by its short nme, you could place a usage statement for it before Begin["`Private`"], or just define it there. Then it will be in the package context, which is placed on the context path when the package is loaded.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ @Kuba, thanks for the edit. $\endgroup$ – David Keith Feb 4 '19 at 19:16

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