Wolfram Computation Meets Knowledge

CopyDefinitions

Contributed by: Richard Hennigan (Wolfram Research)

Copy the definitions of one symbol to another

ResourceFunction["CopyDefinitions"][f,g]

copies the definitions of f into the definitions of g.

Details and Options

The copied symbol will not be dependent on the original symbol as it would be with Set or SetDelayed.

Examples

Basic Examples

Use the function:

In[1]:=
ClearAll[f, g];
f[x_] := x + 1;
ResourceFunction["CopyDefinitions"][f, g];
Definition[g]
Out[1]=

More Examples

Scope

Retrieve the ResourceFunction:

In[2]:=
ResourceFunction["CopyDefinitions"]
Out[3]=

Retrieve the ResourceObject:

In[4]:=
ResourceObject["CopyDefinitions"]
Out[4]=

Generalizations & Extensions

Using Set or SetDelayed can achieve similar behavior:

In[5]:=
ClearAll[f, g];
f[0] = 1;
f[x_Integer?Positive] := f[x - 1]*x;
g = f;
Table[{f[i], g[i]}, {i, 5}]
Out[5]=

However, the new symbol will still be dependent on the old:

In[6]:=
Definition[g]
Out[6]=
In[7]:=
ClearAll[f];
g[5]
Out[7]=

CopySymbol will eliminate dependencies on the original:

In[8]:=
ClearAll[f, g];
f[0] = 1;
f[x_Integer?Positive] := f[x - 1]*x;
ResourceFunction["CopyDefinitions"][f, g];
Table[{f[i], g[i]}, {i, 5}]
Out[8]=
In[9]:=
Definition[g]
Out[9]=
In[10]:=
ClearAll[f];
g[5]
Out[10]=

Resource History