2
$\begingroup$

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.

$\endgroup$
2
  • 8
    $\begingroup$ Without trying your code: f is created as Test`Private`f in TestFunction, but you are matching against Global`f $\endgroup$
    – Szabolcs
    Commented Apr 5, 2017 at 13:03
  • $\begingroup$ At least closely related: 104584, 112151, 114769 $\endgroup$
    – Kuba
    Commented Apr 5, 2017 at 13:08

1 Answer 1

3
$\begingroup$

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!

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – skromnibog
    Commented Apr 10, 2017 at 6:29
  • 1
    $\begingroup$ It is not a discussion panel. SE format is Clear question <-> valid answers. Your answer didn not fit your question. I moved your clarification. $\endgroup$
    – Kuba
    Commented Apr 10, 2017 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.