The Singleton Incantation in Carbon
Ensure only one instance of a nexus object exists across the entire Carbon matrix.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Singleton
ARCANUM SCHOOL: Abjuration // Leyline Control
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
package Singleton api;
// Carbon's approach to global state is heavily restricted by design.
// We simulate a singleton through module-level variables or static access.
class NexusNode {
var energy_level: i32;
fn GetPower[me: Self]() -> i32 { return me.energy_level; }
fn Pulse[addr me: Self*]() { (*me).energy_level += 1; }
}
var GlobalNexus: NexusNode = {.energy_level = 100};
fn GetNexus() -> NexusNode* {
return &GlobalNexus;
}
The Singleton: The Singular Nexus
The old C++ mages abused Singletons, turning their grimoires into tangled webs of global state and order-of-initialization disasters. Carbon, guided by the Successor Pact, frowns upon unchecked globals.
Yet, when a true, unified Leyline Node is required—a single source of truth for the entire application—the pattern remains. In Carbon, we expose this via strict, statically initialized variables and deliberate accessor functions, ensuring that the initialization order is deterministic and memory remains inviolate.
Runic Tags:
#creational
#carbon
#global state