[swift-evolution] Proposal: Define and call a named nested function in one step.

Amir Michail a.michail at me.com
Wed Feb 17 10:42:41 CST 2016


> On Feb 17, 2016, at 11:06 AM, Stephen Celis <stephen.celis at gmail.com> wrote:
> 
> 1. How would you pass parameters to such a function?
> 2. If the only purpose of the function is to be called immediately, why not write it inline?
> 3. If you really want to separate the logic, why not use a do block? You can even label it if you want:
> 
>    func f() {
>      g: do {
>        ...
>      }
>    }
> 

If at some point in the future you want to call the nested function more than once, it takes less finger typing to do so than with the labeled do.

Also, if a cyclomatic complexity tool treats nested functions as if they were not nested, then you would get a lower cyclomatic complexity for the containing function with my proposal than with a labeled do. 

> 4. Nested functions are generally helpful so that the call site becomes more readable (e.g. you pass the nested function to a `map` or `filter`).
> 
> Stephen
> 
>> On Feb 17, 2016, at 10:58 AM, Amir Michail via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>> Writing nested functions with meaningful names is a good way to document the code while reducing the complexity of the function containing them.
>> 
>> This proposal concerns nested functions that are defined just before they are called (a common thing to do to avoid passing parameters to the nested function):
>> 
>> func f() {
>>>> func g() {
>>   ...
>> }
>> g()
>> ...
>> }
>> 
>> With this proposal, you would be able to just write:
>> 
>> func f() {
>>>> call func g() {
>>   ...
>> }
>> ...
>> }
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 



More information about the swift-evolution mailing list