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

Are strings mutable in Ruby?

Yes, << mutates the same object, and + creates a new one. Demonstration: irb(main):011:0> str = “hello” => “hello” irb(main):012:0> str.object_id => 22269036 irb(main):013:0> str << ” world” => “hello world” irb(main):014:0> str.object_id => 22269036 irb(main):015:0> str = str + ” world” => “hello world world” irb(main):016:0> str.object_id => 21462360 irb(main):017:0>

What is difference between mutable and immutable String in java

Case 1: String str = “Good”; str = str + ” Morning”; In the above code you create 3 String Objects. “Good” it goes into the String Pool. ” Morning” it goes into the String Pool as well. “Good Morning” created by concatenating “Good” and ” Morning”. This guy goes on the Heap. Note: Strings … 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

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

tech