# Missing definition of some parts of function?

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.

• That Print[1] prints 1 is actually not its result, it's a side-effect. Just try Print[1] // FullForm and you get Null. – Sjoerd C. de Vries May 21 '15 at 19:56
• This question can be found here, the answer useful: mathematica.stackexchange.com/q/7502/7251 – jonaprieto May 21 '15 at 21:12
• Possible duplicate: (15870) – Mr.Wizard May 22 '15 at 13:13

AA and BB in the definition of f are local symbols of the Test1context. 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[Test1PrivateAA[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

• Oh, sure. it is a solution. Thanks. – jonaprieto May 21 '15 at 20:18
• 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. – J. M. will be back soon May 21 '15 at 20:22
• I'm just repeating the OP's original pattern, assuming he left the usage string empty to reduce the size of the example. – Sjoerd C. de Vries May 22 '15 at 6:04