[SR-1721] Nested functions need to be defined in code before they are used Created: 9 Jun 2016  Updated: 9 Jun 2016  Resolved: 9 Jun 2016

Status: Resolved
Project: Swift
Component/s: None

Type: Improvement Priority: Medium
Reporter: Mariano Abdala Assignee: Unassigned
Resolution: Won't Do Votes: 1
Labels: None

Attachments: Zip Archive NestedFuncs.playground.zip    

 Description   

One approach to using nested functions is grouping pieces of code, for instance, configuration of several views in a function called `configureViews`.

In such task, the natural way of looking at it would be to call the functions first, as they serve as a sort of summary, and then define the functions later.

Doing so will result in the following error being displayed:
`Use of local variable `[functionName]` before its declaration`

See attached Playground.



 Comments   
Comment by Jordan Rose [ 9 Jun 2016 ]

Since nested functions can capture local variables, the place where they are declared is significant.

Comment by Mariano Abdala [ 9 Jun 2016 ]

Then I would understand that variables are defined before the functions are, and that variables are defined before the functions are invoked, but not that definition of functions need to precede its invocation. At least from a Swift `consumer` side.

Comment by Jordan Rose [ 9 Jun 2016 ]

That's not sufficient when a variable (as opposed to a constant) is captured by value (using a capture list). You have to know when the capture happens to know what the value is.

Comment by Mariano Abdala [ 9 Jun 2016 ]

Cool, just trying to understand. Thanks.

Generated at Tue Oct 16 13:51:06 CDT 2018 using Jira 7.11.1#711001-sha1:ea73d62a147b9e78feacb774553e1635dfb20b0b.