mutable

What is the theory behind mutable and immutable types?

浪尽此生 提交于 2019-12-03 09:35:13
问题 One of the things that I admire about Python is its distinction between mutable and immutable types. Having spent a while programming in c before coming to Python, I was astonished at how easily Python does away with all the complexities of pointer dereferencing that drive me mad in c. In Python everything just works the way I expect, and I quickly realized that the mutable/immutable distinction plays an important part in that. There are still a few wrinkles, of course (mutable function

NSArray @property backed by a NSMutableArray

别来无恙 提交于 2019-12-03 09:21:01
I've defined a class where I'd like a public property to appear as though it is backed by an NSArray . That is simple enough, but in my case the actual backing ivar is an NSMutableArray : @interface Foo { NSMutableArray* array; } @property (nonatomic, retain) NSArray* array; @end In my implementation file ( *.m ) I @synthesize the property but I immediately run into warnings because using self.words is the same as trying to modifying an NSArray . What is the correct way to do this? Thanks! I would declare a readonly NSArray in your header and override the getter for that array to return a copy

Why tuple is not mutable in Python? [duplicate]

安稳与你 提交于 2019-12-03 08:30:40
This question already has answers here : Why are python strings and tuples are made immutable? (6 answers) Possible Duplicate: Why are python strings and tuples are made immutable? What lower-level design makes tuple not mutable in Python? Why this feature is useful? A few reasons: Mutable objects like lists cannot be used as dictionary keys or set members in Python, since they are not hashable . If lists were given __hash__ methods based on their contents, the values returned could change as the contents change, which violates the contract for hash values. If Python only had mutable sequences

How to append or prepend on a Scala mutable.Seq

回眸只為那壹抹淺笑 提交于 2019-12-03 08:06:16
问题 There's something I don't understand about Scala's collection.mutable.Seq. It describes the interface for all mutable sequences, yet I don't see methods to append or prepend elements without creating a new sequence. Am I missing something obvious here? There are :+ and +: for append and prepend, respectively, but they create new collections — in order to be consistent with the behavior of immutable sequences, I assume. This is fine, but why is there no method like += and +=: , like

Is modifying a value type from within a using statement undefined behavior?

帅比萌擦擦* 提交于 2019-12-03 07:01:34
This one's really an offshoot of this question , but I think it deserves its own answer. According to section 15.13 of the ECMA-334 (on the using statement, below referred to as resource-acquisition ): Local variables declared in a resource-acquisition are read-only, and shall include an initializer. A compile-time error occurs if the embedded statement attempts to modify these local variables (via assignment or the ++ and -- operators) or pass them as ref or out parameters. This seems to explain why the code below is illegal. struct Mutable : IDisposable { public int Field; public void

Swift mutable structs in closure of class and struct behave differently

假如想象 提交于 2019-12-03 07:01:29
I have a class(A) that has a struct variable (S). In one function of this class I call a mutating function on struct variable,this function takes a closure. Body of this closure checks the struct variable's name property. Struct's mutating function in turns calls a function of some class(B). This class's function again takes a closure. In this closure's body mutate the struct, i.e. change the name property, and call the closure that was supplied by the first class. When the first class (A) closure is called where we are inspecting the struct's name property, it is never changed. But in step 2

Why is MutableString deprecated in Python?

江枫思渺然 提交于 2019-12-03 05:49:31
Why was the MutableString class deprecated in Python 2.6; and why was it removed in Python 3? The MutableString class was meant to be educational, and not to be used in real programs. If you look at the implementation, you'd see that you can't really use this in a serious application requiring mutable strings. If you need mutable bytestrings, you might consider using bytearray that's available in Python 2.6 and 3.x. The implementation doesn't create new strings every time you modify the old one, so it is much more faster and usable. It also supports the buffer protocol properly so it can be

const_cast VS mutable ? any difference?

我的梦境 提交于 2019-12-03 03:16:48
From my understanding , mutable cancels the constness of a variable Class A { void foo() const { m_a = 5; } mutable int m_a; }; But also const_cast : void print (char * str) { cout << str << endl; } int main () { const char * c = "this is a line"; print ( const_cast<char *> (c) ); return 0; } So , what changes one from the other ? Thanks const_cast cannot cancel constness of an object. const_cast can only remove constness from an access path to an object. Access path is a pointer or a reference to an object. Removing the constness from the access path has absolutely no effect on the object

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

久未见 提交于 2019-12-03 00:35:31
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# would allow state to change in a closure? Example: var foo = "hello"; Action bar = () => Console.WriteLine(foo); bar(); foo = "goodbye"; bar(); This will print "hello" for the first call, but the outside state changes for the second call, printing "goodbye." The

Java Mutable BigInteger Class

旧城冷巷雨未停 提交于 2019-12-02 22:08:01
I am doing calculations with BigIntegers that uses a loop that calls multiply() about 100 billion times, and the new object creation from the BigInteger is making it very slow. I was hoping somebody had written or found a MutableBigInteger class. I found the MutableBigInteger in the java.math package, but it is private and when I copy the code into a new class, many errors come up, most of which I don't know how to fix. What implementations exist of a Java class like MutableBigInteger that allows modifying the value in place? Is their any particular reason you cannot use reflection to gain