# Why is pattern matching different inside package

This works fine when put in a notebook:

TestFunction2[x_] := Column[{Head /@ x, Cases[x, _f], MatchQ[_f] /@ x}];
TestFunction2[{f[1, 2]}]


and produces:

{f}
{f{1,2}}
{True}


However if the function is put in a package, for example:

BeginPackage["Test"]
TestFunction::usage=""
Begin["Private"]
End[]
EndPackage[]


and then used from a notebook:

Needs["Test"];
TestFunction[{f[1, 2]}]


it produces:

{f}
{}
{False}


Why?

Edit:

My example was not detailed enough to explain what I want to achieve. I indeed want to have f that is defined in the context of Test. This is because I want to use it as a type inside the package Test.

This is for example because I want to distinguish between two different arrays and wrap one of them for example in f[{1,2,3}] and another one in g[{1,2,3,}] and thus make distinction how they should be processed in Test.

• Without trying your code: f is created as TestPrivatef in TestFunction, but you are matching against Globalf – Szabolcs Apr 5 '17 at 13:03
• At least closely related: 104584, 112151, 114769 – Kuba Apr 5 '17 at 13:08

The problem was that f is created as TestPrivatef. Modifying the code in the package like this:

BeginPackage["Test"]
TestFunction::usage=""
f::usage=""
Begin["Private"]
`