[swift-evolution] Update the signature of ObjectiveC.autoreleasepool [SR-842]

Dmitri Gribenko gribozavr at gmail.com
Mon Mar 21 01:41:07 CDT 2016


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()
}

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-evolution mailing list