[swift-evolution] Should Swift apply "statement scope" for ARC

Joe Groff jgroff at apple.com
Mon Sep 26 16:42:02 CDT 2016

> On Sep 25, 2016, at 3:19 AM, John Holdsworth <mac at johnholdsworth.com> wrote:
>> On 25 Sep 2016, at 04:07, Michael Gottesman <mgottesman at apple.com <mailto:mgottesman at apple.com>> wrote:
>>>     init(imageProducer:ImageProducer) {
>>>         withExtendedLifetime(CanvasBase()) {
>>>             super.init(javaObject: $0.javaObject)
>>>         }
>>>         image = createImage(imageProducer)
>>>     }
>>> ..but the compiler was having none of it.
>> What was the error? I am assuming that super.init was not in the same function?
> Sorry, I should have included that in the email. The error was:
> error: initializer chaining ('super.init') cannot be nested in another expression
> Is it possible to build in an exception for withExtendedLifetime?

The block structure of `withExtendedLifetime` is a bit of a lie. It looks a bit goofy, but it's just as good to have an empty `withExtendedLifetime` call after the super.init:

init(imageProducer: ImageProducer) {
  let canvasBase = CanvasBase()
  super.init(javaObject: CanvasBase())
  withExtendedLifetime(canvasBase) {}
  image = createImage(imageProducer)

If you look at the standard library implementation of `withExtendedLifetime`, you'll see that all it does is `defer` a call to the underlying internal `_fixLifetime` operation:

https://github.com/apple/swift/blob/master/stdlib/public/core/LifetimeManager.swift#L15 <https://github.com/apple/swift/blob/master/stdlib/public/core/LifetimeManager.swift#L15>

It may be a better design to just expose `fixLifetime` directly instead of trying to tie it to a scope.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160926/d034b67d/attachment.html>

More information about the swift-evolution mailing list