✨ ARCANUM RUNE DIFFICULTY: Apprentice Behavioral

The State

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Form Shifting
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
structural type Phase = Liquid | Solid | Gas

structural type Elemental = Elemental Phase Nat

applyHeat : Elemental -> Elemental
applyHeat el = match el with
  Elemental Solid p -> Elemental Liquid p
  Elemental Liquid p -> Elemental Gas p
  Elemental Gas p -> Elemental Gas p
  
attack : Elemental -> Text
attack el = match el with
  Elemental Solid _ -> "Crush!"
  Elemental Liquid _ -> "Drown!"
  Elemental Gas _ -> "Suffocate!"

The State pattern in object-oriented tongues relies on polymorphic swapping of internal reference classes. Unison approaches this through Algebraic Data Types representing a Finite State Machine. The Phase dictates the elemental’s behavior. Functions simply pattern-match on the current phase, executing the correct logic and transitioning the entity to a newly hashed state in the weave.

Runic Tags: #behavioral #state #unison #algebraic-data-types #fsm