On 29 October 2017 at 14:02, Johannes Weiß <[email protected]> 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)
}
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
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution