[swift-evolution] [Proposal] Scoped resources (like C# using statement)
Michel Fortin
michel.fortin at michelf.ca
Thu Dec 31 15:40:31 CST 2015
Le 30 déc. 2015 à 16:40, Kevin Ballard via swift-evolution <swift-evolution at swift.org> a écrit :
> On Wed, Dec 30, 2015, at 01:33 PM, Joe Groff wrote:
>> Another possibility I've thought of is defining `defer { val }` to guarantee that val remains alive until the defer fires on scope exit. That might let us leave `defer` as the one "guarantee something happens exactly at scope exit" language construct.
>
> Oh that's cute. I'd probably want to say `defer { _ = val }` though, to make it obvious that this is intentional.
Or you could make it even more obvious what this is by giving that feature a name. For instance:
defer { _fixLifetime(val) }
The good thing about this one is that it already works, because that's what `withExtendedLifetime` does internally. Also, if you search a bit, you'll end up here with a nice explanation of what that function does.
https://github.com/apple/swift/blob/8d9ef80304d7b36e13619ea50e6e76f3ec9221ba/stdlib/public/core/LifetimeManager.swift#L45
So why not simply remove the underscore?
--
Michel Fortin
https://michelf.ca
More information about the swift-evolution
mailing list