3 ms·
"immutable" is kind of overloaded term, but what it means in this context is an object itself doesn't mutate when you want to change a value (basically value se
by andrekandre 4y ago
"immutable" is kind of overloaded term, but what it means in this context is an object itself doesn't mutate when you want to change a value (basically value semantics[0] instead of reference semantics[1])
for example (pseudocode)
var newUser = Person(name: "some guy", age: 0)
// newUser is replaced with a new object that is
// initialized with field.name and previous age (0)
// the old object is discarded
newUser.name = field.name
// object is passed as copy-on-write, assuming a 1 sec delay
// it will print the objects values at this point in time
// (age: 0) even if altered later
async(after:1) { print(newUser.age) } // prints 0
// age was changed to 32 a nanosecond later,
// but now a new object again is initialized
// instead of mutated
newUser.age = 32
print(newUser.age) // prints 32
----------
output:
32
0
[0] https://en.wikipedia.org/wiki/Value_semantics https://en.wikipedia.org/wiki/Value_semantics
[1] https://stackoverflow.com/questions/27084007/what-is-value-and-reference-semantics-and-the-difference https://stackoverflow.com/questions/27084007/what-is-value-a...
- andrekandre 4y agois something i wrote ↑ here incorrect or off-topic? would appreciate a correction!