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

Stephen Celis stephen.celis at gmail.com
Wed Feb 17 10:06:43 CST 2016


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 {
        ...
      }
    }

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