Skip to main content
added 356 characters in body
Source Link
Murta
  • 26.4k
  • 6
  • 77
  • 168

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;
]

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?

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;
]
Tweeted twitter.com/#!/StackMma/status/304446058707185664
edited title
Link
Murta
  • 26.4k
  • 6
  • 77
  • 168

Copying one symbol tointo another

Source Link
Murta
  • 26.4k
  • 6
  • 77
  • 168

Copying one symbol to another

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?