4
$\begingroup$

So in this answer we see the only use I know of of the function Language`SetMutationHandler.

And just to see what it could do I tried it on one version of my OOP frameworks (this one cooked directly into a chemistry package):

SetAttributes[ChemObjectMutationHandler, HoldAllComplete];
ChemObjectMutationHandler[
   Set[sym_Symbol?ChemObjectQ[prop_], newvalue_]
   ] :=
  ChemSet[sym, prop, newvalue];
obj_ChemObject?ChemObjectQ[prop_] :=
  ChemGet[obj, prop];
Language`SetMutationHandler[ChemObject, ChemObjectMutationHandler];

Then I tested it by just trying to assign a property to an object:

co = ChemImport["methyloxirane"];

co["Property"]

Missing["KeyAbsent", "Property"]

co["Property"] = 1

ChemGet[co, "Property"]

1

And it seems to have worked. The fact that the ChemGet call (basically just a pull from a hash table) worked and that the DownValues remain empty is comforting:

co // DownValues

{}

So my question is this, what else can this function handle? I took that usage pattern directly from the linked answer, but not knowing how the function sets its hooks / what hooks it's setting it's hard to know what else I can do. What forms of mutability can it introduce?

Also there are three other functions like that:

Names["Language`*Mutation*"]

{"Language`HandleMutation", "Language`HasMutationHandlerQ", \
"Language`MutationFallthrough", "Language`SetMutationHandler"}

Does anyone we know that they do?


Side note: I recall a question where the OP wanted exactly this type of behavior a few years ago, but couldn't find it. Anyone know what it was called?

$\endgroup$
0