9

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!"?

rcollyer
  • 33,976
  • 7
  • 92
  • 191
jlmr
  • 155
  • 4
  • 1
    Welcome to [Mathematica.se]! I cannot tell you how much I appreciate your efforts in correctly marking up your question. Although, I did fix the issue with context names, which involved wrapping the whole thing in double grave marks, not single ones, but most people are not aware of that. I would encourage you to register your account so you can more fully participate here. Again, welcome, and I hope to see you here again. – rcollyer Dec 06 '12 at 20:43
  • Thanks for the welcome @rcollyer! And for fixing my source, I couldn't figure out how to do that. – jlmr Dec 06 '12 at 21:01
  • You're welcome. Three questions. 1. Why are you setting UpValues[...] directly, instead of using either UpSet (^=) or TagSet (name /: test[name] = ...) directly? 2. Have you tried setting a usage string on test, or if you have considered that why doesn't it do what you need? 3. Do you have any purpose for Module other than to provide convenient structure, as it doesn't do anything in your code? – rcollyer Dec 06 '12 at 21:12
  • 2 answers and 1 question in return: 1. I didn't know about UpSet. It seems to produce the same results though. What's the difference between setting the values directly and using UpSet? 2. Where would I set a usage string on test? In the function definition or somewhere else? 3. It's true that Module 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 include Module, since it also deals with scopes. – jlmr Dec 06 '12 at 21:16
  • There's no difference between what you're doing and UpSet, just that UpSet 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 for testFunction. 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.
  • – rcollyer Dec 06 '12 at 21:45
  • Excellent question style: clearly formatted, minimal working example, ... I'm impressed (+1)! Welcome here. Anyway, I think it would be good if you add another Details section to your question where you explain a bit further what your goal is. I have the feeling that just answering your question is not the best possible help you can get. Maybe we can help you with the overall approach. – halirutan Dec 06 '12 at 22:05