mjdi

Search documentation

Search the mjdi v0.10.0 documentation

mjdi docsv0.10.0latest

Alias semantics (P0-09)

Aliases in mjdi are explicit, deterministic, and test-pinned by

Source: mjdi/docs/alias-semantics.md

Aliases in mjdi are explicit, deterministic, and test-pinned by tests/test_alias_semantics.mojo. Mojo's value type system has no subtyping, so an alias cannot hand out an instance of a different type — and mjdi's aliases do not pretend to.

What an alias actually does

Registry().register_alias[From, To]() binds requests for From to the lifetime configuration of To's provider:

  1. resolving From constructs From through its own Injectable contract (its own Inject fields resolved against the registry);
  2. the resulting instance is cached in the alias's own cache slot with the effective lifetime of the target;
  3. the target's own provider, cache slot, and lifecycle are completely independent.

An alias binds lifetimes, not instances. To share an implementation, compose instead: give the alias source an Inject[SharedImpl] field.

comptime reg = (
    Registry()
    .register_value[Config]()
    .register[UserRepository, provider_scope.singleton]()
    .register_alias[UserRepoView, UserRepository]()   # view of the repo
)
var app = Container[type_of(reg)]()
var view = app.resolve[UserRepoView]()   # constructed via its own contract,
                                         # cached with the target's lifetime

The exact rules (each pinned by a test)

RuleBehavior
ConstructionThe alias source is constructed through its own Injectable.create (with post_construct, like every class provider). The target is never constructed or cached by resolving the alias — pinned: resolving a singleton-target alias twice reports exactly one construction, and the target's slot stays empty.
Cache ownershipThe alias owns a separate slot per resolving context (container or scope). Two scopes each cache their own alias instance.
Lifetime inheritanceThe alias's effective lifetime is the target's (resolve_scope follows the target, recursively): transient target → fresh per resolve, nothing cached; singleton target → one per context; scoped target → one per scope, container resolution rejected.
Cleanupclose()/close_scope() releases the alias's cached instance exactly once, with pre_destruct for lifecycle services. The unresolved target's slot is untouched. When both were resolved, each slot is released exactly once — no shared or duplicated ownership, no double destruction.
PrecedenceAliasing an already-registered type overrides it for that type (last registration wins; dispatch is head-first). The overridden provider node remains in the chain for graph validation.
Alias chainsNot supported. The target must be a class provider, so an alias can never bind to another alias — chains are impossible by construction. MJDI-E017 fires at registration (tests/compile_fail/cf_alias_target_alias.mojo) and again at validation.
CyclesImpossible by construction (class-only targets terminate every chain); the MJDI-E019 diagnostic still guards validation against malformed registries.
Invalid targetsUnregistered target → MJDI-E001; non-class target → MJDI-E017; non-Injectable source → MJDI-E018.
Scoped target from containerRejected with MJDI-R001, exactly like any scoped provider.

Registration-time vs validation-time checks

The alias rules are enforced twice: eagerly at register_alias (when the method body executes — see docs/compile-time-validation.md for why comptime registration also needs the second pass) and again during graph validation, which Container construction always monomorphizes. Both reports carry the same stable IDs (MJDI-E001/E017/E019), per the validation output contract.

Compatibility

All of the above is 0.9.0's inherited behavior, now explicitly documented and test-pinned rather than implied. 0.9.0 adds no alias behavior change; the additions are the hardening tests, the cf_alias_target_alias fixture, and this page.