2
$\begingroup$

I have:

BeginPackage["Test1`"];
ClearAll[f];
f::usage = "";
Begin["`Private`"];
f[x_AA]:= 1;
f[x_BB]:= 2; 
End[];
EndPackage[];

When later, on a notebook, after the call package successful with:

<<Test1`

I always get from evaluated:

x = AA[3];
f[x]

It should be: 1. But, actually, It is f[AA[3]]. The issue is same if I used x = BB[4];

Any idea? I don't realize how to make it works.

$\endgroup$
3

1 Answer 1

5
$\begingroup$

AA and BB in the definition of f are local symbols of the Test1`context. If you want to use them in the Global context you have to export them from the package or you have to precede them with their context:

f[Test1`Private`AA[3]]

1

Or

BeginPackage["Test1`"];
ClearAll[f];
f::usage = "";
AA::usage = "";
BB::usage = "";
Begin["`Private`"];
f[x_AA] := 1;
f[x_BB] := 2;
End[];
EndPackage[];

f[AA[3]]

1

$\endgroup$
3
  • $\begingroup$ Oh, sure. it is a solution. Thanks. $\endgroup$ May 21, 2015 at 20:18
  • 2
    $\begingroup$ It actually suffices to include the line AA; BB; just before the Begin[] if you don't want to bother with writing usage messages for them. $\endgroup$ May 21, 2015 at 20:22
  • $\begingroup$ I'm just repeating the OP's original pattern, assuming he left the usage string empty to reduce the size of the example. $\endgroup$ May 22, 2015 at 6:04

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.