2

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`"]
TestFunction[x_]:=Column[{Head/@x,Cases[x,_f],MatchQ[_f]/@x}];
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.

Kuba
  • 136,707
  • 13
  • 279
  • 740
skromnibog
  • 151
  • 6

1 Answers1

3

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

BeginPackage["Test`"]
TestFunction::usage=""
f::usage=""
Begin["`Private`"]
TestFunction[x_]:=Column[{Head/@x,Cases[x,_f],MatchQ[_f]/@x}];
End[]
EndPackage[]

will make it work.

Thank you Szabolcs for pointing this out!

Thanks to Kuba for pointing out the linked examples!

Kuba
  • 136,707
  • 13
  • 279
  • 740
skromnibog
  • 151
  • 6
  • Of course, the naming must be done carefully. I prefer not to change the question as the reader then can follow the discussion as it was. – skromnibog Apr 10 '17 at 06:29
  • 1
    It is not a discussion panel. SE format is Clear question <-> valid answers. Your answer didn not fit your question. I moved your clarification. – Kuba Apr 10 '17 at 06:36