Swift functions and captured mutable data


(Nicholas Outram) #1

I’m writing a summary of Swift functions in my iTunes U course. I just wanted to clear a few things as I can find no definitive information online to formally back up what I say.

In my summary, I say something along these lines:

“A reference to a function can be likened to a reference to a data structure, and in many ways is similar to an instance of a class. There is immutable program code, and state, some of which might be mutable. Therefore, in a multithreaded environment, you need to treat any function that captures and mutates data in the same way as shared mutable state in order to avoid creating race conditions”

· Is there a description of how a Swift function type is structured in memory?
· Is it ‘reasonable’ to liken a function to a class instance in this way? (given this is only supposed to be an analogy).
· Do I assume correctly about the dangers of capturing data in multithreaded code?

Cheers

Nick