immutability

Is guava's ImmutableXXX really immutable?

泄露秘密 提交于 2019-11-30 04:29:15
问题 I have been using guava for some time now and truly trusted it, until I stumbled of an example yesterday, which got me thinking. Long story short, here it is: public static void testGuavaImmutability(){ StringBuilder stringBuilder = new StringBuilder("partOne"); ImmutableList<StringBuilder> myList = ImmutableList.of(stringBuilder); System.out.println(myList.get(0)); stringBuilder.append("appended"); System.out.println(myList.get(0)); } After running this you can see that the value of an entry

Check for mutability in Python?

本小妞迷上赌 提交于 2019-11-30 04:23:34
Consider this code : a = {...} # a is an dict with arbitrary contents b = a.copy() What role does mutability play in the keys and values of the dicts? How do I ensure changes to keys or values of one dict are not reflected in the other? How does this relate to the hashable constraint of the dict keys? Are there any differences in behaviour between Python 2.x and Python 3.x? How do I check if a type is mutable in Python? 1) Keys must not be mutable, unless you have a user-defined class that is hashable but also mutable. That's all that's forced upon you. However, using a hashable, mutable

Why is it okay that this struct is mutable? When are mutable structs acceptable?

拈花ヽ惹草 提交于 2019-11-30 04:14:40
Eric Lippert told me I should "try to always make value types immutable" , so I figured I should try to always make value types immutable. But, I just found this internal mutable struct, System.Web.Util.SimpleBitVector32 , in the System.Web assembly, which makes me think that there must be a good reason for having a mutable struct. I'm guessing the reason that they did it this way is because it performed better under testing, and they kept it internal to discourage its misuse. However, that's speculation. I've C&P'd the source of this struct. What is it that justifies the design decision to

python tuple is immutable - so why can I add elements to it

左心房为你撑大大i 提交于 2019-11-30 03:59:15
问题 I've been using Python for some time already and today while reading the following code snippet: >>> a = (1,2) >>> a += (3,4) >>> a (1, 2, 3, 4) I asked myself a question: how come python tuples are immutable and I can use an += operator on them (or, more generally, why can I modify a tuple)? And I couldn't answer myself. I get the idea of immutability, and, although they're not as popular as lists, tuples are useful in python. But being immutable and being able to modify length seems

What is the most efficient implementation of arrays with functional updates?

早过忘川 提交于 2019-11-30 03:59:15
I need an array-like data structure with the fastest possible functional update. I've seen a few different implementation of flexible arrays that provide me with this property (Braun, Random Access Lists) but I'm wondering if there is an implementation that is specifically optimized for the case when we are not interested in append or prepend - just updates. Jean-Cristophe Filliâtre has a very nice implementation of persistent arrays, that is described in the paper linked at the same page (which is about persistent union-find, of which persistent arrays are a core component). The code is

Updating the array object in React state using immutability helper

倖福魔咒の 提交于 2019-11-30 03:39:29
I am updating an object within an array in React state using immutability helper . The object I want to modify is nested: this.state = { a: { b: [{ c: '', d: ''}, ...] } } I want to update the prop c within the nth element of b using immutability helper. The equivalent code without the immutability helper is: const newState = Object.assign({}, this.state); newState.a = Object.assign({}, newState.a); newState.a.b = newState.a.b.slice(); newState.a.b[n] = Object.assign({}, newState.a.b[n]); newState.a.b[n].c = 'new value'; this.setState({ newState }); I know the above code is a bit ugly. I am

what is “failure atomicity” used by J bloch and how its beneficial in terms of immutable object?

非 Y 不嫁゛ 提交于 2019-11-30 03:38:35
just came across below statement as benefit of immutable object Immutable object always have “failure atomicity” (a term used by Joshua Bloch) : if an immutable object throws an exception, it’s never left in an undesirable or indeterminate state. can any one explain it in more detail and why is it so? Bloch's "Failure atomicity" means that if a method threw an exception, the object should still be usable afterwards. Generally, the object should be in the same state as it was before invoking the method. In the case of an immutable object, you gain that simply from the fact that it's immutable.

In Scala, difference between final val and val [duplicate]

青春壹個敷衍的年華 提交于 2019-11-30 03:07:24
This question already has an answer here: Scala final vs val for concurrency visibility 2 answers In Scala, what is the difference between val a = 1 and final val fa = 1 final members cannot be overridden, say, in a sub-class or trait. Legal: class A { val a = 1 } class B extends A { override val a = 2 } Illegal: class A { final val a = 1 } class B extends A { override val a = 2 } You'll get an error such as this: :9: error: overriding value a in class A of type Int(1); value a cannot override final member wingedsubmariner In Scala, final declares that a member may not be overridden in

What is the difference between immutable and const variables in Rust?

廉价感情. 提交于 2019-11-30 02:49:35
I learned that if a variable is not explicitly declared mutable using mut , it becomes immutable (it cannot be changed after declaration). Then why do we have the const keyword in Rust? Aren't they same? If not, how do they differ? Matthieu M. const , in Rust, is short for constant and is related to compile-time evaluation . It shows up: when declaring constants: const FOO: usize = 3; when declaring compile-time evaluable functions: const fn foo() -> &'static str These kinds of values can be used as generic parameters: [u8; FOO] . For now this is limited to array size, but there is talk, plans

Define a python dictionary with immutable keys but mutable values

南楼画角 提交于 2019-11-30 02:45:55
问题 Well, the question is in the title: how do I define a python dictionary with immutable keys but mutable values? I came up with this (in python 2.x): class FixedDict(dict): """ A dictionary with a fixed set of keys """ def __init__(self, dictionary): dict.__init__(self) for key in dictionary.keys(): dict.__setitem__(self, key, dictionary[key]) def __setitem__(self, key, item): if key not in self: raise KeyError("The key '" +key+"' is not defined") dict.__setitem__(self, key, item) but it looks