Skip to main content
1 of 2
Emilio Pisanty
  • 10.3k
  • 1
  • 37
  • 71

How can I pass replacement rules to a package-defined function and have it understand their context?

I am writing a package where I would like to be able to pass replacement rules to a function inside the package and have the function manipulate its symbols using those replacement rules. However, I'm having some trouble implementing it.

(I'm rather baffled because the files used to work and no longer do, coinciding with a change from v10 to v10.1. However, I would be surprised if that is actually the cause as I feel the MWE below completely encapsulates the problem and it looks like a language issue, so instead it may be that I ran the files differently in ways that are hard to reconstruct.)

The specific thing I have in mind is this. Make two files, testpackage.m containing

BeginPackage["Test`"];
replace::usage = "Returns var/.rules.";
Begin["`Private`"];
replace[rules_] := ReplaceAll[var, rules]
End[];
EndPackage[];

and testnotebook.nb containing

Needs["Test`", "/path/to/testpackage.m"]

replace[var-> 0]
replace[Test`Private`var -> 0]

Open the notebook file on a fresh session and load the package. I would like the usage replace[var-> 0] to return 0 - that is, I would like replace to be able to tell var to zero itself. However, it returns Test`Private`a, which upon closer examination of the code is in fact a pretty reasonable thing for it to return. The reasonableness of this is compounded by the fact that replace[Test`Private`var -> 0] will return 0 as expected.

As I said, the usage replace[var->0] used to work fine for my bigger package, and indeed it can work fine in some circumstances (which I cannot pin down exactly, and which I have not been able to reproduce in this MWE yet) if the notebook file is opened on the same Mathematica instance (and thus kernel session) as the package file, with the package file (in practice, a .m auto-generated from a notebook) having been used to run the definition of replace.

So: I have some wonky code, which is probably returning the results it needs to but not the results I want it to, and I would like to amend it to return it to the desired behaviour. In particular: is there some way to take the call

ReplaceAll[var, rules]

and make it forget contexts for a minute?

Alternatively, I'm painfully aware of the possibility that this simply means that I've completely misunderstood the concept of contexts, and that what I want simply isn't doable. This would mean that the behaviour I want, for example, can only be produced by e.g. adding the symbol a to the Test` context, such as by adding the line

a::usage = "a is the variable that gets replaced";

below the usage message for replace. I'm reluctant to take such a step as the package is relatively big (and organically grown, so somewhat messy and without comprehensive unit tests, unfortunately) and I'm afraid of breaking something. If there are no shortcuts then I'm OK with it, but I would like to know if there's a hackier way to get the behaviour I want.

Emilio Pisanty
  • 10.3k
  • 1
  • 37
  • 71