The Stack Overflow podcast is back! Listen to an interview with our new CEO.
2 fixed code formatting
source | link

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] returns test[a]
  • and TestPackagePrivatetest[a]TestPackage`Private`test[a] returns the expected "hello!"

How do I change testFunction so that test[a] also returns "hello!"?

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] returns test[a]
  • and TestPackagePrivatetest[a] returns the expected "hello!"

How do I change testFunction so that test[a] also returns "hello!"?

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] returns test[a]
  • and TestPackage`Private`test[a] returns the expected "hello!"

How do I change testFunction so that test[a] also returns "hello!"?

1
source | link

How to define UpValues in a different context from within a package

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] returns test[a]
  • and TestPackagePrivatetest[a] returns the expected "hello!"

How do I change testFunction so that test[a] also returns "hello!"?