3 ms·
Don’t forget that behind the scenes a value type might actually be used as a reference type when the code is compiled, for performance reasons, but conceptually
by jb1991 3y ago
Don’t forget that behind the scenes a value type might actually be used as a reference type when the code is compiled, for performance reasons, but conceptually the language encourages you to think of everything as value types when possible.
A big part of the semantics in the language is “copy on write”.
- GRiMe2D 3y agoSwift actually passes value types by reference on closures (anonymous functions) if they aren't explicitly listed in capture group. It's per spec. It allows you to write closures with states: var count = 0 let closure = { count += 1 print("Current count=", count) } closure() // Current count= 1 closure() // Current count= 2 closure() // Current count= 3 let capturedVariableClosure = { [count] in print("Current count=", count) //count += 1 // syntax error, count is const variable. Use var count in capture group } capturedVariableCount() // Current count= 3 closure() // Current count= 4 capturedVariableCount() // Current count= 3
- jb1991 3y agoI think you meant to call your second closure `capturedVariableCount`