Skip to main content
26 votes

How to make Mathematica variables declarative instead of just-in-time?

You are looking for $NewSymbol which is run every time a new symbol is created. For example, let say you only want x, ...
rcollyer's user avatar
  • 34k
18 votes
Accepted

Second argument of BeginPackage with nested package loading

I've certainly encountered this behavior before. While I can't speak authoritatively, I'd think this is as designed, although it does introduce certain inconsistency. I also think that this issue is a ...
Leonid Shifrin's user avatar
17 votes
Accepted

Import package with correct symbol contexts

...
Kuba's user avatar
  • 137k
16 votes
Accepted

Attempts at backwards compatibility

If you just want backwards compatibility, there is no need to create any symbols in the System context. It seems like a bad idea to do such things. A package should create symbols only within its ...
Szabolcs's user avatar
  • 235k
14 votes
Accepted

How to make Mathematica variables declarative instead of just-in-time?

Edit: method extended for multiple contexts and unlocking mehtod added. Let's protect whatever is a new symbol. In old answer I've manually excluded symbols matching ...
Kuba's user avatar
  • 137k
13 votes
Accepted

Where does a package have to be loaded?

tl;dr You need to call Needs before GetBoundaryMesh definition so it can be parsed (found in correct context) correctly or you ...
Kuba's user avatar
  • 137k
13 votes
Accepted

How to make Mathematica automatically find packages in subfolders of the Applications directory?

The straightforward answer is: Do not try to do this!! If it were possible, it would break things. There is a specific (and complicated) way Mathematica resolves file names, which allows the ...
Szabolcs's user avatar
  • 235k
13 votes
Accepted

What is the accessibility of a package's `Private` context variables?

So how does MyFunction know the value of abc at the delayed function call (when it is called) if the ...
Szabolcs's user avatar
  • 235k
12 votes
Accepted

FullForm with context for each symbol?

To not mess with $ContextPath you can Block it just for that evaluation. e.g. ...
Kuba's user avatar
  • 137k
12 votes
Accepted

How to list all user-defined symbols?

User defined symbols/functions are in the Global` context: a = 1; b[x_] := a x^2; c[x_] := f[x]; Names["Global`*"] {"a", "...
corey979's user avatar
  • 24k
12 votes
Accepted

What is the correct to have package A use package B in its private section without making B visible to the user?

I use the following pattern. Package B will not be made visible to the user when A is loaded, but it will be usable internally by A. ...
Szabolcs's user avatar
  • 235k
11 votes

Second argument of BeginPackage with nested package loading

What features I/we/developers need: allowing (only) MyPackage` to use particular non System` packages e.g. using ...
Kuba's user avatar
  • 137k
11 votes
Accepted

Package functions and symbolic calculations

Short explanation It returns 0 because you are doing something like: ...
Kuba's user avatar
  • 137k
11 votes

For any symbol, how can I get the full context-qualified name of the symbol as a string?

This is a variation of Leonid's answer that avoids the dependence on a context like "Test`" that must be empty: ...
ilian's user avatar
  • 25.5k
10 votes

What is the most convenient way to read definitions of in-memory symbols when we don't have the source files? (Spelunking tools)

This answer addresses hiding the context marks only, not spelunking. You could use the following functions to view output without contexts marks: ...
Carl Woll's user avatar
  • 131k
10 votes
Accepted

What Does Compile`SetIterate Do?

It sets an iterator: cfun = Compile[{{n, _Integer}}, Table[If[x == 2, Compile`SetIterate[x, 4]]; x, {x, n}] ] Note that 2 ...
Michael E2's user avatar
  • 236k
9 votes

Global context seems to be shared between kernels when using Clear?

If you look at the definition of NotebookEvaluate, by means of Needs["GeneralUtilities`"]; GeneralUtilities`PrintDefinitions[NotebookEvaluate] you see that the ...
Rolf Mertig's user avatar
  • 17.2k
9 votes
Accepted

How does Needs["foo`"] find the file that defines context "foo`"?

This post answers specifically the title question: How does Needs["foo`"] find the file that defines context "foo`"? ...
Szabolcs's user avatar
  • 235k
9 votes
Accepted

Context inside Do[...]-loop

Begin["mycontext`"] affects the parser, not the evaluator. These are separate expressions: Begin["mycontext`"]; ...
Szabolcs's user avatar
  • 235k
9 votes

How to set default for Notebook's Default Context

Here is a programmatic way of what Jason has pointed: CurrentValue[$FrontEnd, CellContext] = Notebook This setting will persists across sessions so you can use <...
Kuba's user avatar
  • 137k
9 votes

ThrowAway Environment and Scoping

You can block $Context and then read your file: ...
Carl Woll's user avatar
  • 131k
9 votes
Accepted

packages will not load

One way: AppendTo[$Path, "~/Desktop/Mathematica/MyPackages"] Get["SamplePackage.m"] bruh[3] (* 10 *) Nicer way: Create a new folder <...
murray's user avatar
  • 11.9k
8 votes
Accepted

Contexts and parallelization

I am not 100% comfortable about this aspect of parallelization, so this is not going to be a full answer, but here are some hints: In order for a piece of code to run in parallel, all definitions it ...
Szabolcs's user avatar
  • 235k
8 votes
Accepted

Begin["`Private`"] vs Module

Reading How symbol lookup actually works will teach you that your code produces such definitions: ...
Kuba's user avatar
  • 137k
8 votes
Accepted

How to automatically do a computation, Quit[], restart and do a new computation

You can start a fresh extra kernel programmatically from your current notebook using MathLink. You can then load packages in that kernel, perform necessary calculations, get the result back to your ...
Ray Shadow's user avatar
  • 7,836
7 votes

How does Needs["foo`"] find the file that defines context "foo`"?

The problem is not that Mathematica can not find the file, which it would indicate with another message. The problem is that Needs insists in your package to create ...
7 votes

How can I use CurrentValue in packages as it generates symbols in current context?

my own current favorite workaround would be this: ...
Albert Retey's user avatar
  • 23.6k
7 votes

What is the accessibility of a package's `Private` context variables?

All symbols are created at load time, so when you do: ...
b3m2a1's user avatar
  • 46.9k
7 votes

What is the accessibility of a package's `Private` context variables?

Begin["`Private`"]; sets the current $Context to "CustomPackage `Private`". This causes two ...
Henrik Schumacher's user avatar
7 votes
Accepted

How to make a contextual variable?

The idea If you adhere to the convention that the size argument is always first in all your functions, then what you ask for can be achieved with some ...
Leonid Shifrin's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible