Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to have Mathematica at the notebook level (SetOptions[EvaluationNotebook[], CellContext -> Notebook]) only uses a set of declared variables in the notebook context. Any variables that are not declared would throw a warning on use. A front-end indication (font format) and/or a run-time warning.

For example, Visual Basic as the OPTION EXPLICIT command which throws a warning if any variables are used that have not been explicitly declared. When this command is not set you can create variables on the fly as you can in Mathematica.

I am developing a UI for a process that as many user driven event updates. I have a quite a few variables that are holding state of this process and these are all floating about in the notebook context. It is easy to typo one of the variable names and then it is a nightmare to locate it.

Short of wrapping the entire code of the notebook in one giant module where misspelled names would show blue, how do I get some front-end indication or run-time warning that a variable is undeclared?


Update

Using @rcollyer answer below that pointed me to $NewSymbol I have constructed the following two functions, declareContext and removeDeclareContext, that enable an OPTION EXPLICIT type environment when the Global setting for CellContext is Notebook (this can be done in the Option Inspector).

ClearAll[declareContext];
declareContext[context_String] :=
 Module[{},
  $NewSymbol::undeclared = "`1``2` was not previously declared.";

  If[ValueQ[$NewSymbol::declarativeContexts] == False,
   $NewSymbol::declarativeContexts = ""
   ];

  $NewSymbol::declarativeContexts = 
   StringJoin[$NewSymbol::declarativeContexts, "|", context];

  $NewSymbol :=
   If[ContainsAny[StringSplit[$NewSymbol::declarativeContexts, "|"], {#2}] &&
      ContainsNone[Names[#2 <> "*"], {#1}],
     Message[$NewSymbol::undeclared, #2, #1]] &;
  ]

declareContext sets up

  • a new message to display for undeclared variables,
  • a repository of contexts that are participating in the option explicit setting by using a new message, $NewSymbol::declarativeContexts,
  • the $NewSymbol to a function that makes use of both its parameters (variable and context) to check the context repository and existing variables in the context.

ClearAll[removeDeclareContext];
removeDeclareContext[context_String] :=
 $NewSymbol::declarativeContexts = 
  StringDelete[$NewSymbol::declarativeContexts, "|" <> context]

removeDeclareContext simply removes the context from the repository.

Both of the above can be call on multiple notebooks and notebooks that have not called them will continue to operate with just-in-time variables. These can be placed in a .wl package file to make them easier to use across multiple notebooks.

To use

At the start of your notebook declare your variables by evaluating them in a list. Then call declareContext with the Context; this should be the notebook context with either the notebook or global preference CellContext -> Notebook.

{x, y, z};
declareContext@Context[]

You may now assign values to the declared variables but new variables will error.

In:= x = 4
Out= 4

but

w = 1
$NewSymbol::undeclared: Notebook$$15$56114`w was not previously declared. >>

Turn off declarative variables by calling removeDeclareContext@Context[].

Issues

The functions above prevent declaring scoped variables as in Module and DynamicModule. Therefore they are too restrictive.

Module[{w},
 w = 1
]
$NewSymbol::undeclared: Notebook$$15$56114`w was not previously declared. >>

You can work around this by using a sub-context for scoped variables.

Module[{m`w},
 m`w = 1
]
1

Any ideas to loosen up the functions so that you do not need a sub-context for scoped variables are very welcome

share|improve this question
    
For the context repository, I'd give you another +1, if I could. Clever workaround. – rcollyer yesterday
    
For debugging I'd use a palette with dynamically updated table of symbols which have values. – Kuba yesterday

You are looking for $NewSymbol which is run every time a new symbol is created. For example, let say you only want x, y, and z as symbols, then declare them initially

In[63]:= {x, y, z}
(*Out[1]= {x, y, z}*)

Then, set $NewSymbol to issue a message when it is used, e.g.

In[2]:= $NewSymbol::undeclared = "`1` was not previously declared.";
In[3]:= $NewSymbol := Message[$NewSymbol::undeclared , #1] &

In[4]:= q
(*
During evaluation of In[4]:= $NewSymbol::undeclared: q was not previously declared.
Out[4]= q*)

But, no message is issued with x.

In[5]:= x = 5
(*Out[5]= 5*)

Additionally, you can create your own cell style that will treat variable declarations as expected using a custom CellEvaluationFunction

CellEvaluationFunction -> (Block[{$NewSymbol}, ToExpression[#]]&)

For instance, you can add it to the "Code" cell, e.g.

Cell[StyleData["Code"], 
 CellEvaluationFunction -> (Block[{$NewSymbol}, ToExpression[#]]&)
]
share|improve this answer
3  
Very good answer. (+1) Going to let this sit for a bit just in case. – Edmund yesterday
    
@Edmund never a problem. I like seeing what others come up with, too. – rcollyer yesterday
    
Does $NewSymbol affect all context or just the context it is set in? Since I use the Notebook context I can't have $NewSymbol from notebook A affect $NewSymbol from notebook B. – Edmund yesterday
1  
Good point. It takes two arguments, the second being the context. A filter can be added to that to make it more robust ... how to make it understand what notebook B's context is automatically would take some digging, but if declared in the notebook, a simple comparison with Context[] should be sufficient. – rcollyer yesterday

Let's protect whatever is a new symbol.

In old answer I've manually excluded symbols matching name$digits but that wasn't necessary as according to $NewSymbol details:

$NewSymbol is not applied to symbols automatically created by scoping constructs such as Module.

Begin["Lock`"];

contextLock[] := With[{con = $Context},
   $NewSymbol := If[
      #2 === con
      , ToExpression[#2 <> #1, StandardForm, Protect]
   ]&
];

End[];

x = 2;
y = 3;

Lock`contextLock[]

x = 4;
g = 3;
Set::wrsym: Symbol g is Protected.
Module[{w}, w = 1] 
1  (* and silence*)
w = 2 
Set::wrsym: Symbol w is Protected.

Notice that w was protected already when Module was parsed. It didn't interfere because during evaluation Module created another w$nnn which was omitted by $NewSymbol by default.

share|improve this answer
    
I am at a loss for what you are testing in StringMatchQ. If I Excho@# in StringMatchQ I just get w for the symbol yet you seem to be matching w to a pattern that contains "$" and digit characters. How is this possible? – Edmund 21 hours ago
    
Is there a way to Echo the w$123 from $NewSymbol? It seams to be that #1 in $NewSymbol should read w$123 instead of w. – Edmund 21 hours ago
    
Also I think $NewSymbol has a bug. Documentation says "$NewSymbol is not applied to symbols automatically created by scoping constructs such as Module." However, it clearly is being applied to symbols created by Module. – Edmund 21 hours ago
    
@Edmund w is not created by module. w$123 is. w is created just by parsing the input. – Kuba 21 hours ago
    
Ah, okay. That makes sense. – Edmund 21 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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