mutable

C++ 'mutable' keyword

A while ago I came across some code that marked a member variable of a class with the 'mutable' keyword. As far as I can see it simply allows you to modify a variable in a 'const' method: class Foo { private: mutable bool done_; public: void doSomething() const { ...; done_ = true; } }; Is this the only use of thi...

Mutable vs immutable objects

I'm trying to get my head around mutable vs immutable objects. Using mutable objects gets a lot of bad press (e.g. returning an array of strings from a method) but I'm having trouble understanding what the negative impacts are of this. What are the best practices around using mutable objects? Should you avoid them whenever possible? ...

Unexpected feature in a Python list of lists

I needed to create a list of lists in Python, so I typed the following: myList = [[1] * 4] * 3 The list looked like this: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] Then I changed one of the innermost values: myList[0][0] = 5 Now my list looks like this: [[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]] which is not what...

Why do new instances of a class share members with other instances?

class Ball: a = [] def __init__(self): pass def add(self,thing): self.a.append(thing) def size(self): print len(self.a) for i in range(3): foo = Ball() foo.add(1) foo.add(2) foo.size() I would expect a return of : 2 2 2 But I get : 2 4 6 Why is this? I've found that by doing a=[] in the init, I can ...

Why are mutable structs evil?

Following the discussions here on SO I already read several times the remark that mutable structs are evil (like in the answer to this question). What's the actual problem with mutability and structs? ...

Are there any good reasons why closures aren't immutable in C#?

I've been going over and over this in my head, and I can't seem to come up with a good reason why C# closures are mutable. It just seems like a good way to get some unintended consequences if you aren't aware of exactly what's happening. Maybe someone who is a little more knowledgeable can shed some light on why the designers of C# woul...

strings in C++

hi, I have following questions regarding strings in C++ 1>> which is a better option(considering performance) and why? 1. string a; a = "hello!"; OR 2. string *a; a = new string("hello!"); ... delete(a); 2>> string a; a = "less"; a = "moreeeeeee"; how exactly memory management is handled in c++ when a bigger string is copie...

Caching expensive data in C++ - function-scoped statics vs mutable member variables

Hi, I've got a relatively expensive data-fetching operation that I want to cache the results of. This operation is called from const methods, roughly like this: double AdjustData(double d, int key) const { double factor = LongRunningOperationToFetchFactor(key); return factor * d; } I'd like AdjustData to remain const, but I want ...

Problem searching a NSMutableArray

Basically, I have a UISearchBar searching an NSMutableArray of stories that make up an RSS feed, and when you select a story, it loads in my app's UIWebView. It's difficult to explain, but I have a list of entries 1, 2, 3, and 4 and you search for '4'. 4 will be the first entry in the now-filtered list of data, right? You'd think that by...

Is 'mutable' the same as the word 'modifiable' in relation to strings?

When we talk about strings as being mutable, is this synonymous with using the word 'changeable' or 'modifiable' or is there some additional nuance to explain why this jargon is used instead of a simpler word like 'modifiable'? ...

Passing NSMutableArray to other classes

I have created an NSMutableArray in the implementation of my class loginController. The mutable array contains a set of strings. I want to pass the mutable array with its objects to other classes within my cocoa-project. What is the best way to pass the array? ...

Mutable or immutable closures

In an imperative, object orients language, would make more sense to have mutable or immutable closures? For example: int i=5; function() f={print(i);}; f(); i=6; f(); If the closure is mutable, this would print: 5 6 If it is immutable, it would print: 5 5 I realize that even with immutable closures, you could still do this: cl...

Mutable Value Objects / Sharing State (and beer brewing!)

I'm a rusty programmer attempting to become learned in the field again. I've discovered, fitfully, that my self-taught and formal education both induced some bad habits. As such, I'm trying to get my mind around good design patterns, and -- by extension -- when they're wrong. The language is Java, and here's my issue: I'm attempting to ...

C++ mutable appropriate in this case?

I would like to ask if the use of mutable is appropriate here: #include <iostream> class Base { protected: int x; public: virtual void NoMod() const { std::cout << x << std::endl; } void Draw() const { this->NoMod(); } }; class Derive : public Base { private: mutable int y; public: void NoMod() const { ...

Ruby Equivalent of C++ Const?

I'm learning Ruby in my spare time, and I have a question about language constructs for constants. Does Ruby have an equivalent of the C++ const keyword to keep variables from being modified? Here's some example code: first_line = f.gets().chomp() column_count = first_line.split( %r{\s+} ).size() print column_count, "\n" I'd like to...

When does a mutable state value freed from heap?

On F# WikiBook under Encapsulating Mutable State section, there is a following code snippet. > let incr = let counter = ref 0 fun () -> counter := !counter + 1 !counter;; val incr : (unit -> int) > incr();; val it : int = 1 > incr();; val it : int = 2 > incr();; val it : int = 3 At first, it seemed easy eno...

Dealing with lazy computation in C++ classes

Let's say I have a class: class NumberCollection { public: typedef std::set<int> SetType; typedef SetType::iterator iterator; void insert(int n); iterator begin(); iterator end(); size_t size() const; iterator difficultBegin(); iterator difficultEnd(); size_t difficultSize() const; private: ...

Mutable vs Immutable for parallel applications

In the application I am writing, I need to write lots of base types, which will most likely be immutable. But I am wondering how mutable types compare in parallel applications to immutable ones. You can use locks with mutable objects, right? How does it compare to other techniques used with immutable types in parallel applications? You...

F# Mutable to Immutable

Gday All, I have been dabbling in some F# of late and I came up with the following string builder that I ported from some C# code. It converts an object into a string provided it passes a Regex defined in the attributes. Its probably overkill for the task at hand but its for learning purposes. Currently the BuildString member uses a mu...

The final word on NSStrings: Mutable and Immutable

I've read in several books... and online... about immutable and mutable strings. They claim "immutable strings" can't be changed. (But they never define "change".) Which of these NSStrings could be changed without using NSMutableString? The string contains "catfish"... and I later try to change it to "cat". (Same letters, just shorter....