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.
- Identity
- Whether two names refer to the same object.
- Value equality
- Whether two values are equal by the fields the program chooses.
- SameValueZero
- The comparison used by includes, Set and Map: value for primitives, identity for objects.
Identity and value comparisons
Identity asks whether two access paths reach one object. Value equality asks whether values agree under a comparison rule. That rule may examine every field or only the fields relevant to a particular task. Ordering asks how values compare for sorting and can also define equivalence for an ordered collection.
Equality syntax and library lookups obtain their comparisons from native type and collection rules. A search, set or map therefore needs review alongside a direct comparison: choosing the right fields in one place does not establish that every lookup uses them. State the intended rule first, then identify the comparison each operation actually invokes.
01Same object or same value
What does == ask about two records?
A booking importer reads rows from a reservation export. Importing a corrected note must not create another booking: the same guest on the same date is a repeat. Before accepting rows or reporting the batch’s distinct count, check whether the comparison and collection lookup enforce that rule.
Start with two separately parsed rows that contain the same guest and date. The importer tries its language’s record comparison before adding note handling. Does that operation recognize the repeat, compare the wrong thing, or need an explicit definition?
type Booking = { guest: string; date: string };
const first: Booking = { guest: "ana", date: "11-03" };
const second: Booking = { guest: "ana", date: "11-03" };
console.log(first === second);
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; defaulted comparisons need C++20.
02Which fields count
A changed note tests the identity policy
A corrected export changes window to aisle for the same guest and date. The booking should still be a repeat. Each program now compares the shown whole record; the string-based version serializes its fields first. Predict whether that comparison matches the import policy.
type Booking = { guest: string; date: string; note: string };
const first: Booking = { guest: "ana", date: "11-03", note: "window" };
const second: Booking = { guest: "ana", date: "11-03", note: "aisle" };
console.log(JSON.stringify(first) === JSON.stringify(second));
03What libraries compare
A library lookup chooses its comparison
The importer must check accepted rows before adding another. This reduced list omits notes to isolate the library operation: it already holds a row with the wanted guest and date. Does the lookup find it?
type Booking = { guest: string; date: string };
const booked: Booking[] = [{ guest: "ana", date: "11-03" }];
console.log(booked.includes({ guest: "ana", date: "11-03" }));
04Repair the booking checks
Compare, search, then count distinct bookings
Repair the importer’s pair comparison, accepted-list decision and distinct-count report. The accepted list can contain several guests and dates, so its answer must identify a matching booking rather than unrelated matching fields. The final report must preserve that identity when moving to a set or map.
The lab opens in your reading language and has its own language control. Each file contains its imports, types, reader and complete caller. Later levels supply the earlier correct helper.
Repair the booking checks
Choose the fields, then make searches and sets use them.
Sign in to keep your read marks on every device. Sign in
Was this helpful?