Use copy when two stored values need to change independently.
names ~= {"Priya", "Grace"}
copied_names ~= copy names
~names.push("Linus") catch:
;
Changing names does not add "Linus" to copied_names.
This is different from a normal assignment:
shared_names = names
A normal assignment creates another shared view. copy creates an independent value.
copy works on a stored name or field:
copied_title = copy page.title
Do not apply copy directly to literals, constructor calls or computed expressions; copy accepts places only. Those expressions create new result roots, but existing values stored inside them still follow ordinary shared-reference rules.
copy requests an independent value from an existing place.
items ~= {"a", "b"}
independent_items ~= copy items
~items.push("c") catch:
;
io.line([: original length: [items.length()]])
io.line([: copied length: [independent_items.length()]])
Copy targets
The operand must be a place with stable storage:
- a visible value binding
- a field projection such as
copy config.title - a parenthesised place such as
copy (items)
Literals, templates, constructor calls, function calls and computed expressions are not copy targets.
copied_title = copy config.title
copied_items = copy (items)
These forms are invalid:
copied_number = copy 42
copied_result = copy make_value()
copied_template = copy [: text]
Fresh expressions already create their own result values.
Contract
- Source code does not silently copy an existing value when another name is created.
- When a stored value is copyable and an independent value is required, write
copy. - The copied value keeps the same semantic type.
- The receiving binding chooses its own mutability.
- Copying is separate from ownership transfer and separate from exclusive access.
Expressions that construct a new result from existing inputs are not implicit copies of one input.
total = unit_price + shipping
label = [: [first_name] [last_name]]
Those expressions create new result values through their own language rules. The first computes a numeric sum. The second builds a string through a template.