4

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?

Rohit Namjoshi
  • 10,212
  • 6
  • 16
  • 67
Mateus
  • 1,243
  • 7
  • 14
  • 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. – Henrik Schumacher Feb 04 '19 at 17:58
  • @HenrikSchumacher - I am confused by the first sentence of your comment. – Jason B. Feb 04 '19 at 18:04
  • 2
    but it should be Begin["`Private`"] – Jason B. Feb 04 '19 at 18:05
  • @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. – Henrik Schumacher Feb 04 '19 at 18:29

1 Answers1

4

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.

Kuba
  • 136,707
  • 13
  • 279
  • 740
David Keith
  • 4,340
  • 1
  • 12
  • 28