Terminology (4)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.
- Record
- A value grouping named fields with distinct roles.
- Access path
- A variable, reference or pointer through which code reaches a record.
- Aggregate invariant
- A relationship that must hold among the fields of a complete record.
- Candidate
- A proposed replacement whose complete field values are considered before it is adopted.
Fields form a record
A record groups values by named roles. A field’s type describes the kind of value it holds; an application invariant describes a relationship the whole record must satisfy, such as a lower bound not exceeding an upper bound.
Creating another binding, copying a record and retaining access to an existing record are different operations. Establish which record a field write reaches before applying an edit. When fields must change together, decide validity for the complete proposed record rather than treating each field as an unrelated value.
01Named roles
Fields give each value a role
A monitoring-rule editor lists each rule by ID and shows the accepted interval for a metric’s signed change from its baseline. An operator wants to duplicate an active rule under a new ID, then try a different interval in the duplicate. The editor must keep the active rule available beside that preview.
Before adding the duplicate action, check the rule summary. Its ID identifies the rule; its width comes from the two interval endpoints. The three stored numbers have different jobs.
type Entry = { code: number; low: number; high: number };
const entry: Entry = { code: 4, low: 2, high: 8 };
console.log("code", entry.code, "width", entry.high - entry.low);
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Save one complete example at a time in a fresh folder under the selected filename. Run the shown checking/build command before execution. The browser lab supplies its editor, stdin fixtures and native runner.
02Values and access paths
A second binding may name the same record
The duplicate action changes an ID while the active rule stays visible. This probe binds changed from original, writes through changed, then reports both IDs. Predict whether the first record keeps its ID.
type Entry = { code: number; low: number; high: number };
const original: Entry = { code: 4, low: 2, high: 8 };
const changed = original;
changed.code = 9;
console.log("original", original.code, "changed", changed.code);
03A complete record invariant
Judge the complete candidate when fields change together
An operator moves the interval from 2..6 to 8..10 in one edit. The implementation still writes one field at a time. It measures validity after the first write and after the second; predict both observations.
type Entry = { code: number; low: number; high: number };
const entry: Entry = { code: 4, low: 2, high: 6 };
const candidate = { ...entry };
candidate.low = 8;
const during = candidate.low <= candidate.high;
candidate.high = 10;
const final = candidate.low <= candidate.high;
console.log("during", during, "final", final);
04Practice
Repair the monitoring-rule editor
Complete the monitoring-rule editor: read the rule ID, duplicate a rule under the requested new ID with both bounds intact, then preview a bounds patch. Keep the active rule and the patch input available for comparison.
Entry.code is the rule ID, including ID 0. low and high describe a signed metric delta with low<=high. Missing patch endpoints keep their old values; a present zero is a requested bound. The renamed report identifies the duplicate.
Start the lab
Read one named field, preserve and display a duplicate, then accept or reject a complete bounds proposal.
In the frontend A typed draft can still hold bounds that the application will reject. Optional
A form needs to display an interval while it is being edited. The record type groups its fields, but it does not make low<=high a type-level guarantee. These components keep a local draft and show that condition without publishing an alert configuration.
React replaces its state object through a setter. Svelte’s $state plain object uses a reactive proxy, so a property binding can update that local draft directly. Neither mechanism chooses the editor’s acceptance policy.
'use client';
import { useState } from 'react';
type Draft = { code: number; low: number; high: number };
export default function RuleDraft() {
const [draft, setDraft] = useState<Draft>({ code: 4, low: 2, high: 8 });
return (
<label>
Low for rule {draft.code}
<input
type="number"
value={draft.low}
onChange={(event) =>
setDraft((current) => ({
...current,
low: Number(event.target.value)
}))
}
/>
<output>Ordered bounds: {String(draft.low <= draft.high)}</output>
</label>
);
}
The updater constructs a new outer object with the retained code and high fields. React queues that state update for a subsequent render. Number converts the input text; an empty string converts to 0 here. This excerpt does not validate or save a rule.
Sources: React: updating objects in state,Svelte: $state,Svelte: input bindings.
Sign in to keep your read marks on every device. Sign in
Was this helpful?