spot<T> method

T spot<T>({
  1. String? name,
})

Resolve and return an instance of type T.

Resolution order:

  1. Check local registry
  2. If not found, check parent (if exists)
  3. Continue up the chain until found or throw exception

Type Parameters:

  • T: The type to resolve (must be registered in this scope or parent)

Parameters:

  • name: Optional name qualifier for named instances

Returns: Instance of type T

Throws:

Implementation

T spot<T>({String? name}) {
  final key = SpotKey<T>(T, name);

  // Fast path: check singleton cache first
  if (_singletonCache.containsKey(key)) {
    if (logging) log.v('Cache hit in scope for $key');
    return _singletonCache[key] as T;
  }

  // Check local registry
  if (registry.containsKey(key)) {
    return _resolveLocal<T>(key);
  }

  // Fall back to parent
  if (parent != null) {
    if (logging) log.v('Falling back to parent for $key');
    return parent!.spot<T>(name: name);
  }

  // Not found in this scope or any parent
  final registeredTypes = _getAllRegisteredKeys().map((k) => k.toString()).join(', ');
  throw SpotException(
    'Type $key is not registered in this scope or any parent scope.\n'
    'Registered in this scope: ${registry.keys.map((k) => k.toString()).join(', ')}\n'
    'All registered types: ${registeredTypes.isNotEmpty ? registeredTypes : '(none)'}'
  );
}