immutability

Updating the array object in React state using immutability helper

江枫思渺然 提交于 2019-11-30 12:50:28
问题 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]);

Why Room entities don't work with immutable properties in Android

徘徊边缘 提交于 2019-11-30 12:24:12
I have been exploring Room database object mapping library and I figured something weird. An entity data model cannot have immutable properties, as this answer suggests. But I checked out google's persistent example with kotlin , Room works with immutable properties too. Please check this data class from the example. What could be the reason for this behavior? This could be a good feature if we could create immutable values ( val properties), as this restrict programmers from changing unique identifiers such as ids after an object has been created. It's weird because I can make my Entity class

Immutable Dictionary Vs Dictionary Vs C5 Vs F# - performance

只谈情不闲聊 提交于 2019-11-30 12:20:27
Our application uses plenty of dictionaries which have multi level lookup that are not frequently changing. We are investigating at converting some of the critical code that does a lot of lookup using dictionaries to use alternate structures for - faster lookup, light on the memory/gc. This made us compare various dictionaries/libraries available - Dictionary ( System.Collections.Generics.Dictionary -SCGD), ImmutableDictionary , C5.HashDictionary , FSharpMap . Running the following program with various items count - 100, 1000, 10000, 100000 - shows that Dictionary is still the winner at most

Does StringBuilder become immutable after a call to ToString?

这一生的挚爱 提交于 2019-11-30 11:26:37
I distinctly remember from the early days of .NET that calling ToString on a StringBuilder used to provide the new string object (to be returned) with the internal char buffer used by StringBuilder. This way if you constructed a huge string using StringBuilder, calling ToString didn't have to copy it. In doing that, the StringBuilder had to prevent any additional changes to the buffer, because it was now used by an immutable string. As a result the StringBuilder would switch to a "copy-on-change" made where any attempted change would first create a new buffer, copy the content of the old

Ways to make a class immutable in Python

与世无争的帅哥 提交于 2019-11-30 11:20:00
I'm doing some distributed computing in which several machines communicate under the assumption that they all have identical versions of various classes. Thus, it seems to be good design to make these classes immutable; not in the sense that it must thwart a user with bad intentions, just immutable enough that it is never modified by accident. How would I go about this? For example, how would I implement a metaclass that makes the class using it immutable after it's definition? >>> class A(object): ... __metaclass__ = ImmutableMetaclass >>> A.something = SomethingElse # Don't want this >>> a =

Why are System.Windows.Point & System.Windows.Vector mutable?

感情迁移 提交于 2019-11-30 11:17:37
Given that mutable structs are generally regarded as evil (e.g., Why are mutable structs “evil”? ), are there potential benefits that might have prompted the designers of the .NET framework to make System.Windows.Point & System.Windows.Vector mutable? I'd like to understand this so I can decide whether it would make sense to make my own similar structs mutable (if ever). It's possible the decision to make Point and Vector mutable was just an error in judgment, but if there was a good reason (e.g., a performance benefit), I'd like to understand what it was. I know that I've stumbled over the

Spring Webflow DataBinding to immutable objects via a constructor?

北战南征 提交于 2019-11-30 10:08:43
Is there any way of using an immutable object as a model within a view-state in Spring webflow? I know Spring webflow generally tends towards setters for this kind of thing, but I was wondering if anyone knew of a custom DataBinder or WebDataBinder that could handle binding the data using a constructor? I'm also aware there's this (SPR-1488) JIRA task against the problem, which advocated direct field access. Do people advocate this way of doing things? To me it doesn't quite feel right. Thanks, Stuart I have put an example of how you can do this using Jackson's ObjectMapper (which besides JSON

are user defined classes mutable

♀尐吖头ヾ 提交于 2019-11-30 08:54:12
问题 Say I want to create a class for car , tractor and boat . All these classes have an instance of engine and I want to keep track of all the engines in a single list. If I understand correctly if the motor object is mutable i can store it as an attribute of car and also the same instance in a list. I cant track down any solid info on whether user defined classes are mutable and if there is a choice to choose when you define them, can anybody shed some light? 回答1: User classes are considered

Use of guava immutable collection as method parameter and/or return type

╄→гoц情女王★ 提交于 2019-11-30 08:42:20
I am trying to determine what the best practices would be for an ImmutableList. Below is a simplistic example that will help drive my questions: Ex: public ImmutableCollection<Foo> getFooOne(ImmutableList<Foo> fooInput){ //.. do some work ImmutableList<Foo> fooOther = // something generated during the code return fooOther; } public Collection<Foo> getFooTwo(List<Foo> fooInput){ //.. do some work List<Foo> fooOther = // something generated during the code return ImmutableList.copyOf(fooOther); } public void doSomethingOne(){ ImmutableCollection<Foo> myFoo = getFooOne(myList); ...

Python: why can I put mutable object in a dict or set?

ⅰ亾dé卋堺 提交于 2019-11-30 08:35:47
问题 Given the following example, class A(object): pass a = A() a.x = 1 Obviously a is mutable, and then I put a in a set, set([a]) It succeeded. Why I can put mutable object like "a" into a set/dict? Shouldn't set/dict only allow immutable objects so they can identify the object and avoid duplication? 回答1: Python doesn't test for mutable objects, it tests for hashable objects. Custom class instances are by default hashable. That's fine because the default __eq__ implementation for such classes