4
$\begingroup$

Suppose we have the following package ABC.wl.

BeginPackage["ABC`"]

Begin["DEF`"]

f1 = 0;
f2[x_] := x + 1

End[]

EndPackage[]

Using the package from a notebook yields the following definitions.

Needs["ABC`"]
In[1]:= Definition[DEF`f1]
Out[1]= DEF`f1 = 0
In[2]:= Definition[DEF`f2]
Out[2]= DEF`f2[DEF`x_] := DEF`x + 1

How can DEF`* be "moved" to Global` such that the definitions are of the same form as if they were defined in Global`?

In[1]:= Definition[f1]
Out[1]= f1 = 0
In[2]:= Definition[f2]
Out[2]= f2[x_] := x + 1

Update Some motivation as requested by Szabolcs: this is useful e.g. when you want to print definitions from a context in a package without touching the source code of the package.

$\endgroup$
  • $\begingroup$ Mathematica will warn you if e.g. f1 is present in both the ABC`​ and Global`​ contexts. Do you really want to have the same symbol for different contexts? $\endgroup$ – J. M.'s discontentment May 11 at 14:35
  • $\begingroup$ The context ABC` could be removed after the definitions have been moved to Global` to avoid the conflict. $\endgroup$ – Markus May 11 at 14:41
  • 2
    $\begingroup$ If you really want this, you can do e.g. the following: Block[{$ContextPath = {"System`", "DEF`"}}, Scan[(Context[#] = "Global`") &, Names["DEF`*"]]]. This will automatically make DEF` context empty, and it will be then automatically removed from a lit of available contexts, as per Contexts[]. $\endgroup$ – Leonid Shifrin May 11 at 14:52
  • $\begingroup$ Thank you Leonid. If you like you can write it as an answer. $\endgroup$ – Markus May 11 at 15:03
  • 3
    $\begingroup$ Can you add some context and explain the motivation? $\endgroup$ – Szabolcs May 11 at 15:31
2
$\begingroup$

If you really want this, you can do e.g. the following:

Block[{$ContextPath = {"System`", "DEF`"}}, 
  Scan[
    (Context[#] = "Global`") &, 
    Names["DEF`*"]
  ]
] 

Here, Block was used just to make sure that Names will return short symbol string names, rather than fully-qualified ones.

This will move all symbols from DEF` to Global`, which automatically make DEF` context empty, and it will be then automatically removed from a list of available contexts, as per Contexts[].

| improve this answer | |
$\endgroup$
3
$\begingroup$

On my system, Mathematica 10.1 under Windows, I got an error using Leonid's code:

Context::cxdup: Cannot set Context[DEF`x] to Global`, since a symbol already exists with name x and context Global`. >>

I seem to need something like this to avoid it:

Block[{$ContextPath = {"System`", "DEF`"}}, 
 Scan[(Quiet[Remove @@ {"Global`" <> StringTrim[#, __ ~~ "`"]}]; 
    Context[#] = "Global`") &, Names["DEF`*"]]]
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Apparently, I have not considered the case when one or more of the symbols already exist in Global` , in which case one gets this error. Do you mind if I incorporate in some form your observation in my (updated) answer? $\endgroup$ – Leonid Shifrin May 11 at 15:43
  • $\begingroup$ @Leonid No, of course I don't mind. I thought in case this was a version difference I should post separately. $\endgroup$ – Mr.Wizard May 11 at 20:59

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.