[swift-evolution] Making capturing semantics of local

Johannes Weiß johannesweiss at apple.com
Sun Oct 29 12:40:40 CDT 2017



> On 29 Oct 2017, at 3:01 pm, Mike Kluev <mike.kluev at gmail.com> wrote:
> 
>> On 29 October 2017 at 14:02, Johannes Weiß <johannesweiss at apple.com> wrote:
>> 
>> Definitely not arguing with that. But there are (valid?) cases when you want a recursive closure which doesn’t have a native recursion mechanism and then `fix` can be useful I’d argue. I think more straightforward than
>> 
>>>>>> recursive.c = { x in
>>>>>>        (x == 0) ? 1 : x * recursive.c(x - 1)
>>>>>>    }
>> 
> 
> you can do without "recursive.c":
> 
> var factorial: ((Int) -> Int)!
> 
> factorial = { n in
>         n == 0 ? 1 : n * factorial(n - 1)
> }

That’s nicer, thanks!

> 
> factorial(5) // 120
> 
>  
>> . But fortunately have local functions, I can only recall wanting a recursive closure once.
>> 
> 
> in down to earth practical programming even if i didn't have local functions i probably wouldn't bother abusing closures to implement recursion, would just used what's available: methods. and if there is some extra state to pass from the outer scopes, well, so be it, either via parameters of via some instance variables, etc. wasn't too much of a problem in good old C/C++. in a way, knowing exact state you want to pass and passing it explicitly organises the code and the reasoning about it.
> 
> Mike
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171029/37ae0880/attachment.html>


More information about the swift-evolution mailing list