← Fundamentals
Fundamentals

Mutability and constants

Does a constant name stop the value from changing?

Identify which writes a declaration or parameter type actually permits.

Outcome

Predict whether a constant, read-only or copied name stops rebinding, element changes or both, and keep shared default data unchanged while computing new values.

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.

Rebinding
Making a name refer to a different value. A constant or non-mut binding restricts reassignment; a new shadowing declaration is a different binding.
Mutation
Changing an existing value in place, such as one element of a list, so every name that reaches it sees the change.
readonly
A TypeScript checker modifier on a property or array type. It prevents writes through that type only; it is not a runtime freeze.

Bindings, mutation and access

Rebinding changes the value held by a name; mutation changes an existing value, such as an element in a collection. A restriction on one operation does not automatically describe the other. Constant and read-only declarations must be read according to the selected language and the type they qualify.

Protection also has a boundary. A record, a collection it refers to and the collection’s elements can have different access rules. When a helper receives a value or a view of it, follow that access to the data a write would reach. This lets you decide which data can change for a request and which data later requests must still receive unchanged.

01Name or value

Does the base-price declaration reject a preview write?

A catering quote tool prices a fixed lunch package containing one of every listed component. Staff can preview different per-component discounts, but every request must start from the same base menu. A preview helper writes into those prices. Does calling the menu constant prevent that write?

The base menu starts with components priced at 300 and 450 cents. Reduce the preview failure to one write: the first component becomes 250. Predict whether the declaration prevents that element change; a refused declaration is different from a program that runs and changes the menu.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const prices = [300, 450];

prices[0] = 250;

console.log(prices);
What happens?
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. Several experiments are expected to be refused. C++ uses the track’s C++26 mode; std::span needs C++20.

02How far protection reaches

Does the next preview start from the same prices?

The tool now passes its menu to a helper rather than editing a local list. The interface uses a readonly property, a value copy, a shared reference or a const reference. Does that boundary preserve the caller’s base prices?

This reduced failure subtracts 50 cents from only the first component. Two successive preview calls print that component after each attempt; the second component remains 450. Predict both first-price observations, or the native refusal. The full exercise applies its discount to every component and computes the quote total.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Menu = {
  readonly prices: number[];
};

function discount(menu: Menu) {
  menu.prices[0] -= 50;
}

const menu: Menu = { prices: [300, 450] };

discount(menu);

console.log(menu.prices[0]);
discount(menu);

console.log(menu.prices[0]);
Which first-component prices are printed after the two calls?

03Repair the menu quotes

A local total, independent quotes, then editable order prices

Finish the package quote tool with fresh inputs and complete functions. First repair a running menu total: its accumulator may change even though input prices must not. Then quote successive orders with different per-component discounts, including a discount larger than a component price. Finally prepare writable order prices through a Menu wrapper. The supplied caller overrides one component after the discount, then totals the quote. That local override must not edit the menu or affect a subsequent order.

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 helpers.

Repair the package quotes

Rebinding versus changing, then keeping shared defaults unchanged at the level that matters.

  1. easy · F-MUTABILITY-V4-1 Total the menu
  2. medium · F-MUTABILITY-V4-2 Quote without changing the defaults
  3. hard · F-MUTABILITY-V4-3 Prepare an editable quote
In the frontend A read-only price prop protects the component’s access Optional

The quote panel receives base prices from its parent. An array type marked readonly rejects element writes through that prop in both frameworks. The component can still display the supplied values; this type annotation does not freeze the parent’s array.

Example.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Props = { prices: readonly number[] };

export default function BasePrices({ prices }: Props) {
  return <p>{prices.join(", ")}</p>;
}

readonly number[] rejects prices[0] = 250 through this type. Merely declaring the props object or its property readonly would not make number[] elements read-only. Treat incoming props as inputs when preparing a quote.

Sources: React: passing props,Svelte: $props,TypeScript: ReadonlyArray.

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.