I like to call my package private functions using
mypkg`functionName[...]
i.e. add an explicit mypkg
to each call. This way when looking at code I know which package the function lives in.
But the problem is that, even though the function is private and has no usage::
statement, Mathematica still exports it and the function can now be seen using ??
on the package and can also be called.
Here is MWE. The function parseODE
is private.
BeginPackage["mypkg`"]
dsolve::usage = "mypkg`dsolve ode";
Begin["`Private`"]
dsolve[ode_] := Module[{},
Print["In mypkg`dsolve"];
mypkg`parseODE[ode]
];
parseODE[ode_] := Print["In mypkg`parseODE"];
End[];
EndPackage[];
But now
?mypkg`*
And the private function can now be seen and called
mypkg`parseODE[ode]
(*In mypkg`parseODE *)
Changing the code of the package to be like this (have to restart kernel also)
BeginPackage["mypkg`"]
dsolve::usage="mypkg`dsolve ode";
Begin["`Private`"]
dsolve[ode_]:=Module[{},
Print["In mypkg`dsolve"];
parseODE[ode] (*removed package name from call*)
];
parseODE[ode_]:=Print["In mypkg`parseODE"];
End[];
EndPackage[];
Now the private function parseODE
do not show and can't be called
How to add package name in the call, inside my package, without having each function being exported as a result? This makes it very hard to write large code for me, as I always like to add package name to each call of function. I do this in Maple all the time with no problem. I'd like to do the same in Mathematica.
V 14
Update and workaround
I found that by calling the private function from inside the package using
mypkg`Private`parseODE[ode]
Instead of
mypkg`parseODE[ode]
Then it does not export it.
So I will use this as workaround. This make it little longer to type, but that is OK. I am still not sure why using mypkg`parseODE[ode]
caused the function to be exported. It looks like this is just how packages work in Mathematica? but this is not common in other languages.
So now the code looks like this
BeginPackage["mypkg`"]
dsolve::usage = "mypkg`dsolve ode";
Begin["`Private`"]
dsolve[ode_] := Module[{},
Print["In mypkg`dsolve"];
mypkg`Private`parseODE[ode]
];
parseODE[ode_] := Print["In mypkg`parseODE"];
End[];
EndPackage[];
And
parseODE
? What are you up to? ;) $\endgroup$moduleName:-function(...)
from inside the module where the function is private, and this keeps the function private. I'd like to do the same call formats in Mathematica. I find modules in Maple much easier to understand and use than packages in Mathematica. $\endgroup$mypkg`parseODE[ode]
caused the function to be exported. It looks like this is just how packages work in Mathematica?" Yeah it's the design. By writingmypkg`parseODE[ode]
explicitly, you'll create the symbolparseODE
under themypkg`
context, but the contextmypkg`
is not private because it's created byBeginPackage["mypkg`"]
so it's added to$ContextPath
. In this example, the private context (to be precise, the context that hasn't been added to$ContextPath
) ismypkg`Private`
context created by the lineBegin["`Private`"]
. $\endgroup$Private
to each call now is not fun, but will have to do it if there is no alternative. I know I can just call the function without prefixing its name with anything, but I do not like to do this. In Maple I always prefix the function name with the name of the module, even for private function calls. Thanks, $\endgroup$