Function Repository Resource:

BindToLocalSymbol

Source Notebook

Bind a symbol to a LocalSymbol so its value will automatically persist

Contributed by: Bradley Ashby

ResourceFunction["BindToLocalSymbol"][sym]

binds sym to a LocalSymbol.

ResourceFunction["BindToLocalSymbol"][sym,localsymbol]

binds sym to localsymbol.

ResourceFunction["BindToLocalSymbol"][sym,"name"]

binds sym to LocalSymbol["name"].

ResourceFunction["BindToLocalSymbol"][sym,obj]

binds sym to LocalSymbol[obj].

Details

Binding a symbol allows you to work with the symbol directly while also keeping the value of the LocalSymbol updated with changes.
If a name for the LocalSymbol is not given, the "FullSymbolName" of sym will be used.
Most of the operations that are valid on a LocalSymbol will also work on a bound symbol.
Most "Assignments" performed on the symbol are passed through to operate on the local symbol.
The symbol sym is Protected to make it less likely to break the binding accidentally.
Clear can be used to remove the value without breaking the binding between the symbol and the corresponding local symbol.
Remove can be used to fully clear the values of the symbol and the corresponding local symbol, as well as break the binding between them.
Unset will break the binding and set the value of symbol to be the current value of the local symbol.
The restrictions that apply to LocalSymbol will now apply to the symbol.
ResourceFunction["BindToLocalSymbol"] returns a deferred form of the corresponding LocalSymbol.

Examples

Basic Examples (5) 

Bind a symbol to a LocalSymbol:

In[1]:=
ResourceFunction["BindToLocalSymbol"][foo]
Out[1]=

It initially has no value:

In[2]:=
foo
Out[2]=

Assign it a value and the LocalSymbol will also update:

In[3]:=
foo = 15;
LocalSymbol["Global`foo"]
Out[4]=

The reverse also works:

In[5]:=
LocalSymbol["Global`foo"] = "bar";
foo
Out[6]=

The value of the local symbol will persist between sessions and it can be bound to the symbol again:

In[7]:=
Quit
In[8]:=
{LocalSymbol["Global`foo"], foo}
Out[8]=
In[9]:=
ResourceFunction["BindToLocalSymbol"][foo];
foo
Out[10]=

Scope (3) 

Bind to a specific LocalSymbol:

In[11]:=
ResourceFunction["BindToLocalSymbol"][foo, LocalSymbol["bar"]]
Out[11]=
In[12]:=
foo = "123";
LocalSymbol["bar"]
Out[13]=

Bind to a local symbol with a specific name:

In[14]:=
ResourceFunction["BindToLocalSymbol"][testsymbol, LocalSymbol["tothemoon"]]
Out[14]=
In[15]:=
testsymbol = {a, b, c};
LocalSymbol["tothemoon"]
Out[16]=

Other forms of LocalSymbol can also be used, like a file:

In[17]:=
ResourceFunction["BindToLocalSymbol"][file, LocalSymbol["/tmp/testsym1"]];
file = <|a -> b|>;
{file, LocalSymbol["/tmp/testsym1"]}
Out[19]=

Specify a directory:

In[20]:=
mybase = "/tmp/symbols";
ResourceFunction["BindToLocalSymbol"][relative, LocalSymbol["sym1", mybase]];
relative = "aunt";
{relative, LocalSymbol["sym1", mybase]}
Out[23]=

Use a path specified with File:

In[24]:=
ResourceFunction["BindToLocalSymbol"][filewrapper, LocalSymbol[File["/tmp/testsym2"]]];
filewrapper = "bubble wrap";
{filewrapper, LocalSymbol[File["/tmp/testsym2"]]}
Out[26]=

Properties and Relations (4) 

Bound symbols can be assigned delayed values:

In[27]:=
ResourceFunction["BindToLocalSymbol"][foo];
foo := RandomReal[]
In[28]:=
Table[foo, {5}]
Out[28]=

Bound symbols and their local objects are removed with Remove:

In[29]:=
Remove[foo];
{foo, LocalSymbol["Global`foo"]}
Out[30]=

Bound symbols work with reassignment functions such as Increment:

In[31]:=
ResourceFunction["BindToLocalSymbol"][n]
n = 0;
Out[31]=
In[32]:=
n++;
LocalSymbol["Global`n"]
Out[33]=
In[34]:=
n *= 2;
LocalSymbol["Global`n"]
Out[35]=
In[36]:=
--n;
LocalSymbol["Global`n"]
Out[37]=

Bound symbols also work with functions like AppendTo:

In[38]:=
ResourceFunction["BindToLocalSymbol"][list]
list = {};
Out[38]=
In[39]:=
AppendTo[list, 1]
Out[39]=
In[40]:=
PrependTo[list, -1]
Out[40]=

Possible Issues (3) 

As with LocalSymbol, setting a list of symbols at the same time will not work with a bound symbol:

In[41]:=
ResourceFunction["BindToLocalSymbol"][foo];
{x, foo, LocalSymbol["other"]} = {a, b, c};
{x, foo, LocalSymbol["other"]}
Out[42]=

If a symbol already has a value when using BindToLocalSymbol, it will keep that value and override the stored value in the local symbol:

In[43]:=
LocalSymbol["Global`foo"] = "string";
foo = 30;
ResourceFunction["BindToLocalSymbol"][foo];
{foo, LocalSymbol["Global`foo"]}
Out[44]=

If maintaining the value of the local symbol is desired, clear the value of the symbol first:

In[45]:=
LocalSymbol["s"] = "string"; s = 30;
Clear@s;
ResourceFunction["BindToLocalSymbol"][s, LocalSymbol["s"]];
{s, LocalSymbol["s"]}
Out[46]=

When using Unset to separate the symbol and local symbol, the symbol will be assigned the current value of the local symbol using Set rather than SetDelayed:

In[47]:=
ResourceFunction["BindToLocalSymbol"][foo];
foo := x + 1;
x = 1234;
foo
Out[48]=
In[49]:=
Unset[foo];
{foo, LocalSymbol["Global`foo"]}
Out[50]=
In[51]:=
x = 2;
{foo, LocalSymbol["Global`foo"]}
Out[52]=

Publisher

Bradley Ashby

Version History

  • 1.0.0 – 30 September 2022

Related Resources

License Information