The Singleton
Establishing universal truth within a Starlark evaluation phase.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Singleton
ARCANUM SCHOOL: Abjuration // Constants
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// STARLARK PATTERN //
# Starlark files are evaluated exactly once per build phase.
# Top-level variables inherently act as singletons for the loading phase.
_global_mana_pool = {"current": 1000, "max": 1000}
def get_mana_pool():
return _global_mana_pool
def consume_mana(amount):
if _global_mana_pool["current"] >= amount:
_global_mana_pool["current"] -= amount
return True
return False
True singletons spanning an entire distributed build are impossible by design in hermetic systems. However, during the loading phase of a Starlark environment, the module itself enforces the Singleton pattern. A .bzl file is evaluated exactly once, and its exported symbols are cached and shared across all files that load it. We utilize module-level dictionaries to maintain stateful singletons (where permitted by the host environment, though many dialects freeze them post-load).
Runic Tags:
#creational
#starlark
#hermetic