3
$\begingroup$

If I were to type both individually with // FullForm, It would seem that both are the same: However, in the context for example of a dumpsave:

DumpSave["~/Documents/test.mx", $Context]

DumpSave["~/Documents/test2.mx", Evaluate[Context[]]]

it would seem that the latter works as expected while the former does not. Why is that?

$\endgroup$
  • $\begingroup$ I want to save all the symbols that have been defined in the current context. The latter does exactly that, while the former just does not do anything at all. $\endgroup$ – Patrick.B Feb 3 at 19:03
  • 1
    $\begingroup$ The difference is that $Context does not get evaluated because DumpSave has the attribute HoldRest (see here for details). DumpSave["~/Documents/test.mx", Evaluate@$Context] works. $\endgroup$ – Henrik Schumacher Feb 3 at 19:14
6
$\begingroup$

There are several issues. First of all Context[] is the same here and can be replaced with $Context to make the example clearer. Now:

  1. DumpSave is HoldRest so in case of symbol = 2 it can save that definition instead of seeing 2.

  2. DumpSave["file", symbol] will save defnitions of symbol. So will DumpSave["file", $Context]. It will save information that $Context = whateverthecontextthereis.

If you want to DumpSave["file", "Global`"] you need to explicitly write it or inject it before DumpSave is evaluated. Those are most common ways to do this:

Function[x, DumpSave["file", x]] @ $Context

DumpSave["file", #]& @ $Context

DumpSave["file", Evaluate @ $Context]

With[{ x = $Context }, DumpSave["file", x] ]

In your recent question you had DumpSave["file", { Evaluate @ $Context }]. It didn't work because Evaluate only works on the first level of an expression that has Hold* attribute. This expression here is DumpSave but on the first level it has a string and a list so evaluator does not care about Evaluate.

$\endgroup$
  • $\begingroup$ Thanks a lot for the thorough answer! I understand the mistake much better now. $\endgroup$ – Patrick.B Feb 3 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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