It's 2025, can we have a “lazy let”?

You can see from its example that it can completely replace lazy let, but it is very inelegant and restrictions (for reasons I don't understand, only classes are supported).

class Image {
  let _histogram = AtomicLazyReference<Histogram>()
    ///
  // This is safe to call concurrently from multiple threads.
  var atomicLazyHistogram: Histogram {
    if let histogram = _histogram.load() { return histogram }
    // Note that code here may run concurrently on
    // multiple threads, but only one of them will get to
    // succeed setting the reference.
    let histogram = ...
    return _histogram.storeIfNil(histogram)
  }
}
1 Like