Immutable/Mutable Collections in Swift

Arrays Create immutable array First way: let array = NSArray(array: [“First”,”Second”,”Third”]) Second way: let array = [“First”,”Second”,”Third”] Create mutable array var array = [“First”,”Second”,”Third”] Append object to array array.append(“Forth”) Dictionaries Create immutable dictionary let dictionary = [“Item 1”: “description”, “Item 2”: “description”] Create mutable dictionary var dictionary = [“Item 1”: “description”, “Item 2”: “description”] Append … Read more

Is Integer Immutable

Immutable does not mean that a can never equal another value. For example, String is immutable too, but I can still do this: String str = “hello”; // str equals “hello” str = str + “world”; // now str equals “helloworld” str was not changed, rather str is now a completely newly instantiated object, just … Read more

Why does using `arg=None` fix Python’s mutable default argument issue?

It looks like a_list would still be initialized only once “initialization” is not something that happens to variables in Python, because variables in Python are just names. “initialization” only happens to objects, and it’s done via the class’ __init__ method. When you write a = 0, that is an assignment. That is saying “a shall … Read more

Does the ‘mutable’ keyword have any purpose other than allowing the variable to be modified by a const function?

It allows the differentiation of bitwise const and logical const. Logical const is when an object doesn’t change in a way that is visible through the public interface, like your locking example. Another example would be a class that computes a value the first time it is requested, and caches the result. Since c++11 mutable … Read more