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.
- Destructuring
- Binding several names from parts of one value in a single declaration or assignment.
- Positional binding
- Parts are matched by their order, whatever the names are.
- Named binding
- Parts are matched by field or property name (TypeScript object patterns, Rust struct patterns).
Taking a value apart
A compound value can carry several parts, while a function can return several results. Receiving those parts introduces a mapping: each destination gets a part selected by position, field name or an explicit adapter. The receiving names alone do not tell you which mapping the operation uses.
Destructuring expresses that mapping in a pattern. A function’s multiple results also need an explicit association with their receiving names. Read the selected operation before relying on the meaning of a destination name, and handle an absent part separately from deciding where a present part belongs.
01By position
Does the size label preserve the supplier’s dimension order?
A packing station imports supplier measurements into a box-size report. Staff need width x height in the agreed order before comparing the available boxes. The adapter receives both dimensions in one result. Can its chosen variable names silently put them in the wrong report columns?
Start with the dimension adapter in isolation. size() returns width 640, then height 480. The report prints width x height, but the caller chooses its receiving names in the opposite order. Predict whether those names change the association.
function size(): [number, number] {
return [640, 480];
}
const [height, width] = size();
console.log(`${width}x${height}`);
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. C++ uses the track’s C++26 mode; structured bindings need C++17.
02By name or by order
Does putting the dimensions in a record remove the order dependency?
The importer now carries measurements in a Size record with height before width. The display still needs width x height. Each program uses the adapter shown to receive width and height. Predict its output before assuming that names on the receiving side select the corresponding fields.
type Size = { height: number; width: number };
const size: Size = { height: 480, width: 640 };
const { width, height } = size;
console.log(`${width}x${height}`);
03A missing part
Can an omitted depth consume or suppress a report row?
The report uses depth 1 when the supplier omits it. Reduce that boundary to just a label and an optional depth, keeping the width and height out of this diagnostic. TypeScript, Go and Rust receive the single text value crate. C++ also has the next row, tray 5 9, in its stream. Predict the actual result before applying the report’s default policy.
const [label, depth] = "crate".split(" ");
console.log(label, depth);
04Repair the box report
Sizes, volumes, then records
Complete the packing report with fresh box inputs. First keep the supplier’s width and height attached to the right output names. Next report volumes for every input row, defaulting only missing depth and preserving an explicit zero. The final adapter receives a Box record: its field or nested-tuple order differs from the display, and its absent depth needs the record’s native representation handled correctly.
The raw-volume task accepts an explicit depth of zero. The stored-record task supplies only positive depths when present, so its Go zero sentinel is unambiguous. Each file contains its imports, types, reader and complete caller. The lab opens in your reading language and has its own language control.
Repair the box report
Order, missing parts, then named records unpacked by position.
In the frontend Rename a label prop without changing which field it reads Optional
The packing report’s caption component receives a label prop. Both examples bind that named prop to a local caption and use a placeholder only when the prop is absent or undefined. Property order does not choose the field.
type Props = { label?: string };
export default function BoxCaption({ label: caption = "Unlabelled" }: Props) {
return <p>{caption}</p>;
}
label names the incoming property; caption is the local binding. The default replaces undefined, and an explicitly empty string is kept. A parent update leads to a new render with the new prop.
Sources: React: passing props,Svelte: $props.
Sign in to keep your read marks on every device. Sign in
Was this helpful?