[swift-evolution] Proposal: optionally define nested functions elsewhere

Dany St-Amant dsa.mls at icloud.com
Sun Feb 7 09:35:48 CST 2016


> Le 7 févr. 2016 à 09:32, Amir Michail via swift-evolution <swift-evolution at swift.org> a écrit :
> 
> The problem with nested functions is that they can make the function containing them very long and hard to read.
> 
> So the idea is to separate the body of a nested function from where it is declared.
> 
> For example:
> 
> func f() {
>  var x:Int
>  func g(y:String) {
>    print(“x=\(x), y=\(y)")
>  }  
>  ...
> }
> 
> could be refactored as:
> 
> func f() {
>  var x:Int
>  func g(y:String) // function body elsewhere to avoid clutter in f
>  ...
> }
> 
> func f().g(y:String) {
>    print(“x=\(x), y=\(y)")
> }  

I see two issues with this.

People might mistakenly think from the definition that they could call directly:
f().g("blah")
which they cannot, as g() can only be called within f(); g() is a private function of f() and used variables defined within f().

When changing the code, like renaming variable x, it make it a little harder to find all instance of x to properly asses the impact of the change. A function variable change need to be analyzed as a file scoped change.

Dany
 



More information about the swift-evolution mailing list