✨ ARCANUM RUNE DIFFICULTY: Adept Creational

The Singleton

Ensure a magical construct has only one instance, using the global unified namespace of the weave.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Singleton
ARCANUM SCHOOL: Enchantment // Global Artifacts
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
-- The concept of a mutable Singleton is an anti-pattern in pure Unison.
-- Instead, we define a pure global constant or use an Ability handler to inject a shared environment.

structural type WorldTree = Node Text [WorldTree]

theOneTree : WorldTree
theOneTree = Node "Yggdrasil" []

ability GlobalState v where
  get : () -> v
  
withSingleton : v -> '{GlobalState v} a -> a
withSingleton v f = 
  h : Request (GlobalState v) a -> a
  h = cases
    {GlobalState.get _ -> resume} -> handle resume v with h
    {a} -> a
  handle !f with h

The Singleton pattern is often an artifact of mutable, time-bound computing. In the pure, timeless realm of Unison, true singletons are simply immutable definitions embedded in the content-addressed namespace. When an ongoing, singular context is required throughout a spell’s execution, an Ability such as GlobalState is summoned, allowing the single instance to be seamlessly woven into the tapestry of the executing code.

Runic Tags: #creational #singleton #unison #abilities