5
$\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$
2
  • $\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$ Commented Feb 3, 2019 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$ Commented Feb 3, 2019 at 19:14

1 Answer 1

7
$\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$
1
  • $\begingroup$ Thanks a lot for the thorough answer! I understand the mistake much better now. $\endgroup$ Commented Feb 3, 2019 at 19:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.