[Second review] SE-0390: Noncopyable structs and enums

To extend on this, how can we capture and consume non-copyable values in closures?

Closures today can be executed zero or more times and there is no way of spelling that they are called exactly once. This sounds like it would not be possible to capture and consume a non-copyable type. Lots of APIs actually guarantee that they are executed exactly once, e.g. most of the with* style methods, Result {} and we also have escaping ones like Task {} and EventLoopFuture.whenComplete from SwiftNIO.

non-copyable consuming closures could guarantee that they are not executed more than once but I think we still need another spelling that requires that they are called exactly once.

Edit: non-copyable consuming closures might actually be enough to allow them to capture and consume non-copyable values. They would guarantee that the closure is executed at most once and could therefore consume non-copyable types. If the closure is not called and eventually deinitlized it would then need to deinitlized all captured non-copyable types as well which is already the case today for captured copyable values anyway.

Exactly once closures might still be interesting in combination with @available(*, unavailable) deinit {}:

6 Likes