[swift-users] [Feature Request] Extend the `defer` statement

Geordie Jay geojay at gmail.com
Mon Oct 9 03:49:28 CDT 2017


This feature request is probably one for swift evolution.

I like the idea in theory, but I’d prefer to see the return value
identifier being spelled out, i.e. “returnValue” instead of the symbols.

Jun Zhang via swift-users <swift-users at swift.org> schrieb am Mo. 9. Okt.
2017 um 05:37:

> Yes that's true, but this requires me to set a return value in every
> `early return` statement. I think this should be done by the compiler
> instead of me, to make code cleaner and also less error-prone.
>
> On Mon, Oct 9, 2017 at 11:24 AM, Slava Pestov <spestov at apple.com> wrote:
>
>> I think you can achieve what you want by having the ‘defer’ block capture
>> a mutable local variable. Eg,
>>
>> func doStuff() {
>>   var success = false
>>
>>   defer {
>>     if !success {
>>       // do some additional cleanup
>>     }
>>   }
>>
>>>>   success = true
>>>>
>>   return result
>> }
>>
>> On Oct 8, 2017, at 7:34 PM, Jun Zhang via swift-users <
>> swift-users at swift.org> wrote:
>>
>> Hi dear swift developers,
>>    I am kind of new to swift and I don't know if this feature already
>> exists. And if it exists already please tell me how. Thank you very much!
>>   The feature is to capturing the return value (maybe input value also)
>> of the function. Here is the demo code:
>>
>>     func tableView(_ tableView: UITableView, numberOfRowsInSection
>> section: Int) -> Int {
>>         defer {
>>             if $> >= 0 {
>>                 // table is not empty
>>             }
>>             else {
>>                 // table is empty
>>             }
>>         }
>>         return dataSource.cout
>>     }
>>
>>    I suggest using `$>` as the return value symbol and `$<` as the input
>> parameter symbol.
>>
>>
Also, I’m not sure what “input parameter” means here.

Geordie


   Thank you all and best regards to you!
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users
>>
>>
>>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20171009/ba5763e4/attachment.html>


More information about the swift-users mailing list