# autorelease<R> function

R autorelease<R>(

1. R computation()

)

## Implementation

    R autorelease<R>(R Function() computation) {
      AutoReleasePool.globalPull ??= AutoReleasePool();
      AutoReleasePool.globalPullReferences++;
      bool isAsync = false;
      releasePoolRef() {
        AutoReleasePool.globalPullReferences--;
        if (AutoReleasePool.globalPullReferences == 0) {
          AutoReleasePool.globalPull!.releaseAll();
          AutoReleasePool.globalPull = null;
        }
      }

      try {
        final result = computation();
        if (result is Future) {
          isAsync = true;
          return (result.whenComplete(() {
            releasePoolRef();
          }) as R);
        }
        return result;
      } finally {
        if (!isAsync) {
          releasePoolRef();
        }
      }
    }