← Fundamentals
Fundamentals

Copying, identity, and aliasing

When two names start from the same value, what do they share?

Follow what a copy owns and what later edits can still share.

Outcome

Predict which writes reach an original after assignment, copying or looping in the selected language, and copy each level that will change before editing a draft.

Terminology (3)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Alias
Another access path to the same object or storage, through which a write may be visible to another holder.
Shallow copy
A copy of the outer value whose references, slices or pointers still reach the original nested data.
Deep copy
A copy that also duplicates nested data to the required depth. Whether an operation provides it depends on the field and element types.

Copies, shared identity and moves

Copying creates a value from another value; aliasing gives more than one access path to the same underlying data. Identity asks whether those paths reach the same object. Assignment can copy owned data, copy a reference to shared data or transfer ownership, depending on the selected language and type.

Copy depth matters when values contain other values or references. A separate outer record does not by itself establish that every nested object is separate. Start from the fields an edit will touch and follow each reference, pointer or owning container to its storage. Apply the same reasoning to a loop variable before assuming that writing it changes a saved element.

01Copy, share or move

Can the draft be renamed while published stays available?

An event coordinator needs to try a revised seating layout while keeping the published plan available for comparison. The draft starts from that plan, gets a new title and then moves seats. Which writes belong only to the draft, and what must its copy actually include?

Start with the title-only part of the plan. The draft should acquire its own name while the published title remains available. Each candidate creates draft and writes its title; predict the published observation or the native refusal.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Plan = { title: string };

const published: Plan = { title: "Spring" };
const draft = published;

draft.title = "Spring draft";

console.log(published.title);
After the draft is renamed, which published title is printed, or is the source refused?
TypeScript · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc --noEmit --strict main.ts && node main.ts

Use the selected language’s setup from the first reading and replace the file for each experiment. Some experiments are expected to be refused. C++ uses the track’s C++26 mode; the examples need only C++17.

02How deep a copy goes

Does the row edit cross the draft boundary?

The coordinator now changes a row assignment in the draft. The record includes a list of row numbers; this reduction changes only its first element to locate the sharing boundary. A successful title copy does not answer whether this row write reaches published.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Plan = { title: string; rows: number[] };

const published: Plan = { title: "Spring", rows: [3, 4] };
const draft = { ...published };

draft.rows[0] = 1;

console.log(published.rows);
After the draft’s first row is changed, which observation about published rows follows?
Loading What the row write reaches…

03Loop variables

Will the bulk row adjustment reach the intended elements?

The next edit moves rows toward the front. Before applying it to an entire draft, test what the loop variable means for these integer elements. Each candidate tries to lower 3 and 4 by one. Predict whether it writes the elements, changes a separate local value, or is refused.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const rows = [3, 4];

for (let row of rows) {
  row = row - 1;
}

console.log(rows);
Does this loop update the rows, leave their original values, or fail checking?

04Repair the seating plan

A renamed draft, moved rows, then named seats

Finish the seating tool’s draft edits. First keep the published title while renaming its draft. Then return a draft whose rows move toward the front while the published rows stay in place. Finally apply named moves to saved draft seats, handling repeated moves and absent names without altering published seats.

The input readers, types and complete callers are supplied. Compare both output lines: a correctly changed draft must still leave the published data intact.

Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.