[swift-evolution] Update the signature of ObjectiveC.autoreleasepool [SR-842]
Timothy Wood
tjw at omnigroup.com
Mon Mar 21 09:56:24 CDT 2016
> On Mar 20, 2016, at 11:41 PM, Dmitri Gribenko <gribozavr at gmail.com> wrote:
>
> On Sun, Mar 20, 2016 at 9:32 PM, Timothy Wood via swift-evolution
> <swift-evolution at swift.org> wrote:
>>
>> In preparation for writing a proposal, I wanted to solicit any feedback and general level of interest on addressing SR-842, which proposes modifying ObjectiveC.autoreleasepool to allow a potentially `throw`ing body via a `rethrows` annotation. I have a branch with the very simple change applied and a test. However, Dmitri Gribenko pointed out that it would be even better if the signature was amended to allow for a generic return type:
>>
>> public func autoreleasepool<Result>(@noescape code: () throws -> Result) rethrows -> Result
>>
>> It isn’t clear to me whether it is possible for a wrapper to be written that adds rethrow, since the function needs to compile under the assumption that the passed block does not throw. So, something like this doesn’t actually compile.
>
> Hi Timothy,
>
> I think it should be possible -- or I wouldn't be suggesting it :)
>
> func poolPush() {}
> func poolPop() {}
>
> public func autoreleasepool<Result>(@noescape code: () throws ->
> Result) rethrows -> Result {
> poolPush()
> defer { poolPop() }
> return try code()
> }
Sorry; yes of course it is possible to make stdlib do this. What I meant was whether it is possible for code outside stdlib to easily wrap a autoreleasepool that does not have `rethrow` applied and make one that does. Or, more generically, is it possible in Swift, given only `func f(@noescape body: Void -> Void)` to write `func f(@noescape body: Void throws -> Void) rethrows`? It seemed to me like it isn’t possible, since the wrapper needs to capture the error inside a do/catch in a var and then optionally re-throw it outside the do/catch, but the rethrow can’t be done (as far as I can see) since that seems to make the compiler thing that the function should be marked as `throws` instead of `rethrows`.
In particular, in this case pushPool() and popPool() don’t exist as nice public API:
@warn_unused_result
@_silgen_name("objc_autoreleasePoolPush")
func __pushAutoreleasePool() -> OpaquePointer
@_silgen_name("objc_autoreleasePoolPop")
func __popAutoreleasePool(pool: OpaquePointer)
Either way, I think stdlib should be marked `rethrow` here, but it may make for a more compelling argument for this (and other cases in stdlib that take a function argument) if it is hard or impossible(?) for code outside stdlib to provide a `rethrows` wrapper of code in stdlib.
Thanks!
-tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160321/d7ec1db3/attachment.html>
More information about the swift-evolution
mailing list