16
$\begingroup$

Consider the following simple code which dumps the global context:

var = 5;
DumpSave["test.mx", "Global`"];

Now, in a new notebook, I would like to import var in another context. Trying

Begin["myContext`"]
<< "test.mx"
End[]

does not work because var remains in the global context. Is there a solution?

Thank you.

$\endgroup$

1 Answer 1

24
$\begingroup$

Using Begin and End won't help, because .mx files are lower-level and the way they are loaded is different from normal packages.

I was about to say that this isn't possible, but here is a hack which seems to work:

ClearAll[loadInContext];
loadInContext[context_String,file_String/;FileExtension[file]==="mx"]:=
    Module[{tag},
        Block[{$NewSymbol=Sow[#1,tag]&},
            With[{created=(If[#1==={},{},First[#1]]&)[Reap[Import[file],tag][[2]]]},
                Scan[(Context[#1]=context)&,created]
            ]
        ]
    ]

You can use this as, for example:

loadInContext["MyContext`", "test.mx"}]

It is based on a rather interesting and little-known feature, that the assignment like

Context[sym] = newContext

will move the symbol (together with the global rules possibly attached to it) to a new context.

$\endgroup$
17
  • $\begingroup$ Does this prevent a name collision for Symbols already defined in Global? $\endgroup$
    – Mr.Wizard
    Commented May 1, 2014 at 11:39
  • 1
    $\begingroup$ @Mr.Wizard Thanks for the upvote. Very few people know about this Conext[sym]= thing, I think (I mean, outside of WRI). I discovered it myself some time ago, but I am sure I haven't seen any single mention of this thing anywhere on SO, SE of MathGroup. This was a good opportunity to mention it. $\endgroup$ Commented May 1, 2014 at 11:43
  • 1
    $\begingroup$ @Mr.Wizard Why sadly? Turns out you knew it too. Apparently I was also in that comment discussion, but my lack of response to your comment shows that I knew this by then. This doesn't change the fact that there aren't many people who know about it. It just shows that my memory isn't perfect and I forgot about that comment of yours. $\endgroup$ Commented May 1, 2014 at 11:47
  • 4
    $\begingroup$ @Mr.Wizard as to failing to remember names, when I first met my wife, I could not remember her name to save my life, and she the same. This was despite the fact we were both interested in each other. For some reason, each others' names took a long time to stick. $\endgroup$
    – rcollyer
    Commented May 1, 2014 at 13:01
  • 2
    $\begingroup$ @Rojo I also noticed that. Don't know whether or not this is expected, may be not. But apparently, this opens a possibility to import separate symbols into other contexts, rather than entire contexts - which is quite nice. Not sure though that one can rely on this feature. I will ask around, when I get some time. $\endgroup$ Commented May 3, 2014 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.