[swift-evolution] Update the signature of ObjectiveC.autoreleasepool [SR-842]
Andrew Bennett
cacoyi at gmail.com
Tue Mar 29 18:01:13 CDT 2016
I like this proposal, but I think I agree with Chris, the return value
seems inconsistent.
I am +1 on rethrows.
Your proposed autoreleasepool returns a (non discardable) value and/or
performs a side-effect, depending on how it is being used. Also, most other
standard library functions taking closures are methods that can be chained.
An interesting side-effect of this, not seen elsewhere:
self.list.forEach { print($0) }
self.list.dropLast() // warning: result unused
self.list.removeLast() // no warning, the result is discardable
// all warnings, or no warnings
autoreleasepool { self.list.forEach { print($0) } }
autoreleasepool { self.list.dropLast() }
autoreleasepool { self.list.removeLast() }
Currently the second call provides the appropriate warning. After this
proposal either all or none of these calls will fire that warning
(depending on the annotation). In other cases this is overcome with
multiple functions; i.e. `forEach` versus `map`.
*Solutions and Alternatives*
*Void version*
You can provide a void version of the function, if you can get around the
ambiguous function resolution, but that doesn't help with `removeLast()`
(the following linked proposal helps).
Shameless plug, I've got a proposal that would solve these issues:
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160328/013559.html
*Simplify current usage*
A proposal like this one could improve the current usage:
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160125/008167.html
It would allow us to do things like this (note the `let`):
let x: Int, y: Float, z: Double
autoreleasepool {
(x, y) = calculateSomeStuff()
z = calculateMoreStuff(x, y, 123)
}
It would work with `@discardable`, and the function signature would be
consistent with it having side-effects.
*Discardable annotation*
Maybe you can do this in the future (similar has been discussed, I forget
the details):
autoreleasepool { () @discardable -> Int in ... }
*New function, different name*
If the current proposal did go ahead then I think the old function should
stay around and your function should have a new name, because of the above
reasons. Something like autoreleasepoolvalue although that's quite long.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160330/342e8983/attachment.html>
More information about the swift-evolution
mailing list