PART I
Warm-up
Reconnect with a useful idea before starting state changes with vary. In one or two sentences, describe the input, output or host boundary from the previous exercise that this lesson builds on. If you are arriving directly, skim the worked example first; there is no locked prerequisite.
A rough note is enough. You can revise it after the example.
PART II
Instruction & guided example
Check the path as well as the final count
An acceptable final number does not guarantee that every intermediate state meets a feature’s policy. A stock ledger can dip below zero and later recover. Whether that path is allowed must be defined explicitly; looking only at the return can conceal an invalid transition.
Starting with stock 2, apply correction -5 to obtain -3, then delivery 6 to obtain 3. Reversing the updates gives 2 → 8 → 3. The final arithmetic result agrees, but only the first ordering passes through negative stock. This exercise reveals the path difference without inventing validation behavior.
If the policy forbids negative stock at every transition, the first sequence needs refusal or another specified resolution even though its final 3 is positive. Do not claim vary enforces that invariant; it merely holds the evolving count in this example.
Worked reasoning and supplied artifact
fnc riskyOrder() -> i32 {
vary stock: i32 = 2;
stock += -5;
stock += 6;
return stock;
}Illustrative example from the proposed WillowScript design. The language and WUIX/WMATH/WDB/WAUDIO/WTMP services are not implemented in the supplied source. This site has no interpreter, VM or native host and does not execute or certify the snippet.
Walk through the example, one line at a time
fnc riskyOrder() -> i32 {Read the function name, its parameter names/types and the result type after ->. This is the contract the body must satisfy.
vary stock: i32 = 2;Introduce a binding intended to change. Identify its initial value now so later updates can be traced rather than guessed.
stock += -5;Read the current value, add the right-hand amount and store the new value. A before/after trace makes the state change visible.
stock += 6;Read the current value, add the right-hand amount and store the new value. A before/after trace makes the state change visible.
return stock;Evaluate the following expression using the current values, then send its result to the caller. Explain the value; do not confuse it with printed output.
}Close the nested block or value begun earlier. Match it to its opening boundary to keep the structure readable.
Try the guided change
Trace the shown order and the reversed order. Mark the first row where the nonnegative-stock invariant would fail.
- Write the supplied inputs and initial state before following the example.
- Reconstruct each intermediate calculation or host decision, then compare with the worked artifact.
- Explain why the contrasting alternative fails before attempting the changed inputs in Part IV.
PART III
Take a breather
Step back after the guided example. Look away from the screen, stretch, or reread your prediction. This is an optional learning pause, not a timer or an assessment gate.
When you return, try to name the lesson’s key idea without copying its wording. If that feels difficult, return to Part II and trace one line rather than restarting the entire lesson.
PART IV
Independent practice
Project context: Workshop stock counter. Build a small, understandable change around state changes with vary.
Create two ledgers from stock 4 with a removal of 7 and a delivery of 5 in opposite orders. Compare final states and intermediate validity under a nonnegative-at-each-step policy.
What to produce
Ledgers 4 → -3 → 2 and 4 → 9 → 2, with the exact invalid transition identified and no fabricated automatic rejection.
- State the intended behavior in a sentence before editing.
- Use only source-backed syntax and the host services whose contract you have been given.
- Record a normal case and a boundary or unavailable-resource case.
- Label hand predictions separately from anything actually measured in a supplied toolchain.
Syntax colors are a reading aid, not a compiler. The line you are editing stays uncolored until you move to a different line; capitalization is never changed. Drafts stay on this browser and code is not executed.
Need a hint?
Starting with stock 2, apply correction -5 to obtain -3, then delivery 6 to obtain 3. Reversing the updates gives 2 → 8 → 3. The final arithmetic result agrees, but only the first ordering passes through negative stock. This exercise reveals the path difference without inventing validation behavior.
If the policy forbids negative stock at every transition, the first sequence needs refusal or another specified resolution even though its final 3 is positive. Do not claim vary enforces that invariant; it merely holds the evolving count in this example.
Original learning-path task and reference
In a media review console, demonstrate state changes with vary. Add normal, boundary, and invalid-input cases and explain expected behavior. Include .ws source and a host-resource contract; compare at least one normal case and one edge case.
fnc total() -> i32 {
vary count: i32 = 1;
const bonus: i32 = 2;
count += bonus;
return count;
}Where the original task needs a host API that is not supplied here, write the contract and predictions. Do not invent a runtime result.
PART V
Self-check
Use this check to notice what to revisit. It does not award a private course grade, block another page or claim to verify compiled code. Every answer has a learning explanation.
You may keep exploring even if you are unsure. Use the hint, compare the trace, and revise your explanation.
PART VI
Recap & small task
Bring the idea back to the mod feature you are building. A useful recap connects the mechanism to a decision you can make in your own code.
- Check the path as well as the final count
- An acceptable final number does not guarantee that every intermediate state meets a feature’s policy. A stock ledger can dip below zero and later recover. Whether that path is allowed must be defined explicitly; looking only at the return can conceal an invalid transition.
- Separate source-backed behavior, host requirements and untested predictions.
One small recap task
Write an invariant that must be checked after every update rather than only at return.
This marks your personal practice record only. It is not a certification or an official workspace assessment.
