⚙️ STARLARK RUNE DIFFICULTY: Apprentice Behavioral

The Command

Encapsulating build instructions into executable, hermetic tokens.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Command
ARCANUM SCHOOL: Evocation // Invocation
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// STARLARK PATTERN //
def create_build_command(target_name, action_func, **kwargs):
    def _execute():
        # Executes the enclosed action with the captured arguments
        return action_func(target_name, **kwargs)
    return struct(
        target = target_name,
        execute = _execute
    )

def _compile_action(name, opt_level):
    return "Compiling %s at -O%d" % (name, opt_level)

# Usage
cmd1 = create_build_command("core_lib", _compile_action, opt_level=3)
cmd2 = create_build_command("utils", _compile_action, opt_level=1)

# The invoker can execute them blindly later
queue = [cmd1, cmd2]
results = [cmd.execute() for cmd in queue]

The Command pattern is vital for Starlark rule implementations that must plan actions during an analysis phase, but defer the actual semantic meaning of those actions until they are sequentially processed. By bundling a function and its arguments into a struct wrapper, mages can create queues, undo-stacks, or job pools of build commands that are completely agnostic to the underlying implementations they encapsulate.

Runic Tags: #behavioral #starlark #hermetic