Does the ‘mutable’ keyword have any purpose other than allowing a data member to be modified by a const member 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

Is making in-place operations return the object a bad idea?

Yes, it is a bad idea. The reason is that if in-place and non-in-place operations have apparently identical output, then programmers will frequently mix up in-place operations and non-in-place operations (List.sort() vs. sorted()) and that results in hard-to-detect errors. In-place operations returning themselves can allow you to perform “method chaining”, however, this is bad practice … Read more

F#: let mutable vs. ref

I can only support what gradbot said – when I need mutation, I prefer let mutable. Regarding the implementation and differences between the two – ref cells are essentially implemented by a very simple record that contains a mutable record field. You could write them easily yourself: type ref<‘T> = // ‘ { mutable value … Read more

Swift – How to mutate a struct object when iterating over it

struct are value types, thus in the for loop you are dealing with a copy. Just as a test you might try this: Swift 3: struct Options { var backgroundColor = UIColor.black } var arrayOfMyStruct = [Options]() for (index, _) in arrayOfMyStruct.enumerated() { arrayOfMyStruct[index].backgroundColor = UIColor.red } Swift 2: struct Options { var backgroundColor = … Read more

Why the “mutable default argument fix” syntax is so ugly, asks python newbie

This is called the ‘mutable defaults trap’. See: http://www.ferg.org/projects/python_gotchas.html#contents_item_6 Basically, a_list is initialized when the program is first interpreted, not each time you call the function (as you might expect from other languages). So you’re not getting a new list each time you call the function, but you’re reusing the same one. I guess the … Read more