factory property

Resolvable<T> get factory

Returns a new instance of T each time, acting as a factory.

If _constructor throws, the throw is absorbed into a Sync.err(...) just like singleton does — see that getter's doc for the rationale. Re-entrance from inside the constructor is detected and returns a Sync.err(...) instead of recursing.

Implementation

@pragma('vm:prefer-inline')
Resolvable<T> get factory {
  if (_constructing) {
    return Sync.err(
      Err<T>(
        'Lazy<$T>.factory was accessed re-entrantly from inside its own '
        'constructor — circular dependency detected.',
      ),
    );
  }
  _constructing = true;
  try {
    return _constructor();
  } on Err catch (err) {
    return Sync.err(err.transfErr<T>());
  } catch (error, stackTrace) {
    return Sync.err(Err<T>(error, stackTrace: stackTrace));
  } finally {
    _constructing = false;
  }
}