what is the mechanism in closure as an argument in Swift?

I'm a starter on Swift. I'm a little confused about the result of the codes below.
Code Block
class handler {
static var strings: Array<String> = ["a", "b"]
static func printer() -> Void{
print(strings)
}
}
Code Block
/* defined in another file */
struct Caller {
func call(function:() -> Void){
function()
}
}

What will happen if I pass printer() to call() and call the call function? To be more specific, the variable strings is defined in a class and the function printer() will print it out. But the point is, only the printer() function is passed as an argument to a structure defined in another file, the strings variable doesn't get passed. So how does swift cope with this situation and what is the mechanism behind it (if it can run successfully)?

Could you show an example of the code you try ?
Note: handler class should be named Handler per Swift conventions.
In fact here you do not pass a closure but a function.

Here is the mechanism.
You do not pass printer() to call, you pass Handler.printer (without parenthesis)
Then it knows it has to use strings as defined in Handler class.

Here is an example of use:
Code Block
let caller = Caller()
caller.call(function: Handler.printer)

If you change strings:
Code Block
Handler.strings = ["c", "d"]
let caller = Caller()
caller.call(function: Handler.printer)

you get the new array ["c", "d"]

Note: The fact struct is in another file does not matter.

what is the mechanism behind it 

In case of static vars and static funcs, the mechanism is different than the non-static ones.

In Swift, static vars and static funcs are sort of global vars and global funcs just that the namespace of them are confined in the type name.

Code Block
class Handler {
static var strings: Array<String> = ["a", "b"]
static func printer() -> Void{
print(strings)
}
}

The declaration above is equivalent to something like this:
Code Block
var Handler_strings: Array<String> = ["a", "b"]
func Handler_printer() -> Void {
print(Handler_strings)
}


You know that you can declare global variables in Swift and use it from anywhere it is visible. So, it can run successfully.
Code Block
class Handler {
static var strings: Array<String> = ["a", "b"]
static func printer() -> Void{
print(strings)
}
}
struct Caller {
func call(function:() -> Void){
function()
}
}
let caller = Caller()
caller.call(function: Handler.printer)
//->["a", "b"]



If you define your handler non-static like this:
Code Block
class MyHandler {
var strings: Array<String> = ["a", "b"]
func printer() -> Void{
print(strings)
}
}

Then the mechanism behind it is completely different.

You can pass the instance method printer through the instance of MyHandler seemingly the same way as static case:
Code Block
let handler = MyHandler()
caller.call(function: handler.printer)
//->["a", "b"]

But in this case, the method reference handler.printer returns a closure which implicitly captures self -- the instance itself.
So, print(strings) works as expected by referencing self.strings.

If you have not studied capturing of Swift closures yet, please read the section Capturing Values of the Swift book.


what is the mechanism in closure as an argument in Swift?
 
 
Q