Function Repository Resource:

InheritDefinitions

Source Notebook

Inherit a symbol's definitions

Contributed by: Nik Murzin

ResourceFunction["InheritDefinitions"][source,target]

make the target symbol inherit all definitions from the source symbol.

Details

InheritDefinitions defines special rules for the target symbol that fallback to definitions for the source symbol, making source almost indistinguishable in its behaviour.
Attributes are the only part of the symbol's definition that are not inherited, but just copied.

Examples

Basic Examples (7) 

Define a down value for a symbol:

In[1]:=
ClearAll[a, b]
a["x"] = 123

Create a new symbol and inherit all current and consequent definitions:

In[2]:=
ResourceFunction["InheritDefinitions"][a, b]

Create more definitions:

In[3]:=
a["y"] = 456;
a[x_][y_] := x + y

The second symbol has all definition of the first:

In[4]:=
{b["x"], b["y"], b[2][3]}
Out[9]=

Inherit UpValues:

In[10]:=
ClearAll[a, b, f]
ResourceFunction["InheritDefinitions"][a, b]

f[a] ^:= a["Value"]^2

b["Value"] = 13;

f[b]
Out[14]=

Use inherited definition in pattern matching:

In[15]:=
ClearAll[T, a, b, f]
ResourceFunction["InheritDefinitions"][T, a]
ResourceFunction["InheritDefinitions"][T, b]

f[g : T, h : T] ^:= g["Value"] + h["Value"]

a["Value"] = 13;
b["Value"] = 42;

f[a, b]
Out[21]=

Inherit OwnValues:

In[22]:=
ClearAll[a, b, c]
ResourceFunction["InheritDefinitions"][a, b]

a /; c = 1;
a /; ! c = 2;

{Block[{c = True}, b], Block[{c = False}, b]}
Out[23]=

Inherit formatting:

In[24]:=
ClearAll[a, b]
ResourceFunction["InheritDefinitions"][a, b]

Format[a] := a["Format"];

a["Format"] = "A";
b["Format"] = "B";

{a, b}
Out[25]=

Inherit Options and Default values:

In[26]:=
ClearAll[a, b]
ResourceFunction["InheritDefinitions"][a, b]

Default[a] = 123;
Options[a] = {"Option" -> 234};

Replace[b[], b[x_., OptionsPattern[]] :> {x, OptionValue["Option"]}]
Out[27]=

Inherit NValues:

In[28]:=
ClearAll[a, b]
ResourceFunction["InheritDefinitions"][a, b]

a["Value"] = 123;
N[a, args___] := N[a["Value"], args]
b["Value"] = 234;

N[b, 12]
Out[29]=

Inherit Messages:

In[30]:=
ClearAll[a, b]
ResourceFunction["InheritDefinitions"][a, b]

a::msg = "Hello ``";

Message[b::msg, "World"]

Scope (3) 

Only a single definition per type (DownValues, UpValues etc.) is added to the target symbol:

In[31]:=
ClearAll[a, b]
ResourceFunction["InheritDefinitions"][a, b]

a[1] = 1;
a[2] = 2;
a[3] = 3;

Length /@ {DownValues[a], DownValues[b]}
Out[36]=

The inheriting symbols behaves as if it has the all the DownValues:

In[37]:=
b /@ {1, 2, 3}
Out[37]=

Recursive definitions can be inherited:

In[38]:=
ClearAll[f, g]
ResourceFunction["InheritDefinitions"][f, g]

f[1] = 1;
f[n_] := n f[n - 1]

g[10]
Out[39]=

Self-dependencies work no matter how deep they are:

In[40]:=
ClearAll[a, b]
ResourceFunction["InheritDefinitions"][a, b]

setAValue[x_] := a["Value"] = x
a["Set", x_] := setAValue[x]
b["Set", 42];

{a["Value"], b["Value"]}
Out[41]=

Possible Issues (2) 

Positional Default values doesn't work:

In[42]:=
ClearAll[f, g]
ResourceFunction["InheritDefinitions"][f, g]

Default[f] = 123;

Default[g]
Out[43]=
In[44]:=
ClearAll[f, g]
ResourceFunction["InheritDefinitions"][f, g]

Default[f, 2] = 123;

Default[g, 2]
Out[45]=

Attributes are not inherited, therefore setting source attributes wouldn't propagate to a target:

In[46]:=
ClearAll[f, g]
SetAttributes[f, HoldAll]
ResourceFunction["InheritDefinitions"][f, g]

Attributes[g]
Out[47]=
In[48]:=
SetAttributes[f, HoldAllComplete]
Attributes[g]
Out[49]=

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 23 August 2023

Related Resources

License Information