Replace
Overview
In-place node replacement for tree-shaped data during TreeRewrite transforms.
Locates a node under a root container and swaps it for a new value without rebuilding the entire tree.
Qwiratry::Mold.make calls this service when a transformer composes
TreeRewrite. Replacement is identity-based: the old node must be the exact
object currently present under the root, not merely an equal value. That keeps
rewrites predictable when multiple nodes have the same contents.
The service supports the container shapes used by the built-in tree walker:
positional children, associative children, and direct associative values. If
the root itself is being replaced, compatible containers are mutated in place so
external references to the root remain valid.
Methods
instance()
method instance(--> Qwiratry::Tree::Replace)
Returns the shared replacement service instance.
replace-node(Mu $old, Mu $new, Mu $root)
method replace-node(Mu $old, Mu $new, Mu $root --> Bool)
Parameters
$old
The existing node or value to find in the tree.
$new
The replacement value to install in place of the old node.
$root
The traversal root that provides the data context for the plan.
Replaces $old with $new under $root.
Returns true when a replacement occurred. Undefined inputs and nodes that cannot be found return false; callers can use that to decide whether a TreeRewrite mold actually mutated the input structure.
!replace-in-parent(Mu $parent, Mu $old, Mu $new)
method !replace-in-parent(Mu $parent, Mu $old, Mu $new --> Bool)
Parameters
$parent
The parent container inspected while searching for the node to replace.
$old
The existing node or value to find in the tree.
$new
The replacement value to install in place of the old node.
Replaces $old within a discovered parent container.
The method handles positional slots, associative children arrays, and
direct hash values. It returns false when the parent has no supported slot for
the old node.
!merge-into-container(Mu $container, Mu $new)
method !merge-into-container(Mu $container, Mu $new --> Bool)
Parameters
$container
The mutable container that should receive the replacement contents.
$new
The replacement value to install in place of the old node.
Merges a replacement into the root container when the root itself matched.
Hash roots receive the new hash keys and values; positional roots are spliced to contain the new list. Other shape changes return false because they cannot preserve the original root object identity.