I would like to know how can I completely copy one Symbol to another. When I say copy, a refer to UpValues
, DownValues
, FormatValues
.. and so on.
I created this function to do that, but I don't know if it's a good practice.
SetAttributes[copy,HoldFirst];
copy[new_Symbol,org_Symbol]:=Module[{},
ClearAll@new;
UpValues@new=UpValues@org/.org:> new;
DownValues@new=DownValues@org/.org:> new;
FormatValues@new=FormatValues@org/.org:> new;
SetAttributes[new,Attributes[org]];
]
So I can copy some symbol b
into a
using a~copy~b
.
There is a simpler way to do that? Or this approach is ok?
Update
Thanks for all comments. This is the evolution of the function above:
SetAttributes[copy,HoldFirst];
new_~copy~org_:=With[{prop={Attributes,UpValues, OwnValues, DownValues, SubValues, NValues, FormatValues, Messages,Options}},
ClearAll@new;
Set[#@new,#@org/.HoldPattern@org:>new]&~Scan~prop;
]