🌐 REASONML PACT DIFFICULTY: Adept Behavioral

State in ReasonML

Finite state machines mapped to variants.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Metamorphosis
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// REASON PATTERN //
type state = Idle | Running(int) | Finished;

let step = (s) =>
  switch (s) {
  | Idle => Running(0)
  | Running(5) => Finished
  | Running(n) => Running(n + 1)
  | Finished => Finished
  };

Rather than polymorphic objects representing states, Reason uses exhaustive ADTs. The compiler guarantees that no phantom state can ever be reached.

Runic Tags: #reason #state #behavioral