I'm trying to create a function (in a package) to import a lot of data and create a symbol with UpValues to return specific results about the data. I've got it working fine in a regular notebook, but when I define the function within a package I can't get it to work properly.
A simple example of my problem:
TestPackage.m:
BeginPackage["TestPackage`"];
testFunction::usage= "testFunction[] does all kind of interesting things.";
Begin["`Private`"]
testFunction[name_Symbol]:=
Module[{},(
ClearAll[name];
UpValues[name]=
{
test[name]->"hello!"
};
Protect[name];
)]
End[]
EndPackage[]
I then call the function in a regular notebook:
Needs["TestPackage`", "/path/to/TestPackage.m"]
testFunction[a]
test[a]
returnstest[a]
- and
TestPackage`Private`test[a]
returns the expected"hello!"
How do I change testFunction
so that test[a]
also returns "hello!"
?
UpValues[...]
directly, instead of using eitherUpSet
(^=
) orTagSet
(name /: test[name] = ...
) directly? 2. Have you tried setting ausage
string ontest
, or if you have considered that why doesn't it do what you need? 3. Do you have any purpose forModule
other than to provide convenient structure, as it doesn't do anything in your code? $\endgroup$UpSet
. It seems to produce the same results though. What's the difference between setting the values directly and usingUpSet
? 2. Where would I set a usage string ontest
? In the function definition or somewhere else? 3. It's true thatModule
doesn't serve any purpose here. It does however in the actual function I'm trying to write, which also needs a couple of temporary variables to store results. I thought it wise to includeModule
, since it also deals with scopes. $\endgroup$UpSet
, just thatUpSet
is the common way of accomplishing what you're doing, so more readily recognizable. 2. Set the usage string in the preamble of the package like you do fortestFunction
. 3. I figured that was the reason, but I encountered at least one person who believed it was performing localization while using it like in your code. $\endgroup$