Documentation
¶
Index ¶
- Constants
- Variables
- func Methods(objects ...any) (ret []any)
- func Provide[T any](v T) *T
- type CallResult
- type Fork
- type Inject
- type InjectStruct
- type Module
- type Reset
- type Scope
- func (s Scope) AllTypes() iter.Seq[reflect.Type]
- func (scope Scope) Assign[T any](ptr *T)
- func (scope Scope) Call(fn any) CallResult
- func (scope Scope) CallValue(fnValue reflect.Value) (res CallResult)
- func (scope Scope) Fork(defs ...any) Scope
- func (scope Scope) Get[T any]() T
- func (scope Scope) GetType(typ reflect.Type) reflect.Value
- func (scope Scope) InjectStruct(target any)
- func (scope Scope) Reset() Scope
- func (scope Scope) ToDOT(w io.Writer) error
- func (scope Scope) TryGet[T any]() (o T, ok bool)
- func (scope Scope) TryGetType(typ reflect.Type) (reflect.Value, bool)
Constants ¶
const TheoryOfCallResult = `` /* 553-byte string literal not displayed */
TheoryOfCallResult documents how call return values reach their targets.
const TheoryOfConstructors = `` /* 1150-byte string literal not displayed */
const TheoryOfLazyInitialization = `` /* 889-byte string literal not displayed */
TheoryOfLazyInitialization documents the design rationale for dscope's lazy initialization mechanism. Provider functions are evaluated on first access; results are cached and shared across all consumers within the same scope. A panicking provider must NOT permanently cache the failure — subsequent accesses must re-invoke the provider to reproduce the original error, ensuring that transient provider failures are always diagnosable and never leave the system in an unrecoverable or misleading state.
const TheoryOfModuleMethodDiscovery = `` /* 712-byte string literal not displayed */
TheoryOfModuleMethodDiscovery documents how Methods expands a module object into provider functions and which inputs are rejected.
const TheoryOfModules = `` /* 890-byte string literal not displayed */
TheoryOfModules documents the module pattern: grouping providers as methods on a struct that embeds dscope.Module.
const TheoryOfScopeAssignment = `` /* 1095-byte string literal not displayed */
TheoryOfScopeAssignment documents the retrieval semantics shared by the assignment entry points.
const TheoryOfScopeCore = `` /* 1505-byte string literal not displayed */
TheoryOfScopeCore documents the fundamental model of dscope: an immutable, type-keyed container of lazily evaluated definitions.
const TheoryOfScopeDefinitions = `` /* 1137-byte string literal not displayed */
const TheoryOfScopeFork = `` /* 1304-byte string literal not displayed */
TheoryOfScopeFork documents the semantics and typical uses of Fork.
const TheoryOfScopeForkFlatten = `` /* 732-byte string literal not displayed */
const TheoryOfScopeForkValue = `` /* 655-byte string literal not displayed */
const TheoryOfScopeInjectStruct = `` /* 992-byte string literal not displayed */
TheoryOfScopeInjectStruct documents struct field injection: how fields are selected and how the built-in binding behaves.
const TheoryOfScopeInvocation = `` /* 687-byte string literal not displayed */
TheoryOfScopeInvocation documents the semantics of scope.Call and the validation applied to call targets.
const TheoryOfScopeReset = `` /* 806-byte string literal not displayed */
TheoryOfScopeReset documents the semantics and typical use of Reset.
const TheoryOfScopeResetValue = `` /* 674-byte string literal not displayed */
const TheoryOfScopeVisualization = `` /* 279-byte string literal not displayed */
const TheoryOfTypeGranularity = `` /* 570-byte string literal not displayed */
Variables ¶
var ErrBadArgument = errors.New("bad argument")
var ErrBadDefinition = errors.New("bad definition")
var ErrDependencyLoop = errors.New("dependency loop")
var ErrDependencyNotFound = errors.New("dependency not found")
var Universe = Scope{}
Universe is the empty root scope.
Functions ¶
Types ¶
type CallResult ¶
func (CallResult) Assign ¶
func (c CallResult) Assign(targets ...any)
func (CallResult) Extract ¶
func (c CallResult) Extract(targets ...any)
Extract extracts results by positions
type Fork ¶
Fork is the type of a scope's Fork method, bound to the scope that provides it. It is always provided as a built-in dependency.
type InjectStruct ¶
type InjectStruct func(target any)
type Reset ¶
type Reset func() Scope
Reset is the type of a scope's Reset method, bound to the scope that provides it. It is always provided as a built-in dependency.
type Scope ¶
type Scope struct {
// contains filtered or unexported fields
}
Scope represents an immutable dependency injection container. Operations like Fork create new Scope values.
func New ¶
New creates a new root Scope with the given definitions. Equivalent to Universe.Fork(defs...).
func (Scope) Assign ¶
Assign retrieves the value of type T from the scope and writes it to the provided pointer. It panics if ptr is nil or if the type is not found. It's safe to call Assign concurrently.
func (Scope) Call ¶
func (scope Scope) Call(fn any) CallResult
Call executes the given function `fn`, resolving its arguments from the scope. It returns a CallResult containing the return values of the function. Panics if argument resolution fails or if `fn` is not a function.
func (Scope) CallValue ¶
func (scope Scope) CallValue(fnValue reflect.Value) (res CallResult)
CallValue executes the given function value after resolving its arguments from the scope. It returns a CallResult containing the function's return values. It panics with ErrBadArgument if fnValue is invalid, nil, or not a function.
func (Scope) Fork ¶
Fork creates a new scope by layering the given definitions (`defs`) on top of the current scope's definitions. The result is a new branch of the same definition lineage: scopes have no child-parent relationship, and the original scope is never mutated. Fork handles overriding existing definitions and ensures values are lazily initialized.
Definitions can be provider functions or pointers to values. When a pointer is provided, the value it points to is copied; subsequent changes to the original variable will not affect the value in the scope. To provide a shared singleton, use a provider function that returns a pointer.
func (Scope) Get ¶
Get is a type-safe generic method to retrieve a single value of type T. It panics if the type is not found or if an error occurs during resolution.
func (Scope) GetType ¶
GetType retrieves the value of the given type from the scope and returns it as a reflect.Value. It panics with a structured dependency-not-found error if the type is not defined in the scope, mirroring the generic Get[T].
func (Scope) InjectStruct ¶
func (Scope) Reset ¶
Reset returns a new Scope in which every value will be recomputed the next time it is requested. The original scope is unaffected.
Reset is O(1): it wraps the value stack in a lazy reset layer rather than eagerly iterating all definitions. Fresh initializers are created on demand only for types that are actually accessed, so untouched types incur zero overhead. Once a provider is re-evaluated in the reset scope the result is cached, preserving the at-most-once evaluation guarantee.
func (Scope) TryGet ¶
TryGet is a type-safe generic method to retrieve a single value of type T. Unlike Get, it does not panic when the type is missing from the scope: it returns (zero, false) instead. Panics raised by provider evaluation still propagate.
func (Scope) TryGetType ¶
TryGetType retrieves the value of the given type from the scope as a reflect.Value. Unlike GetType, it does not panic when the type is missing from the scope: it returns the zero reflect.Value and false instead. Panics raised by provider evaluation still propagate.