immutability

Immutable or not immutable?

霸气de小男生 提交于 2019-11-28 06:57:06
Ok, as I understand it, immutable types are inherently thread safe or so I've read in various places and I think I understand why it is so. If the inner state of an instance can not be modified once the object is created there seems to be no problems with concurrent access to the instance itself. Therefore, I could create the following List : class ImmutableList<T>: IEnumerable<T> { readonly List<T> innerList; public ImmutableList(IEnumerable<T> collection) { this.innerList = new List<T>(collection); } public ImmutableList() { this.innerList = new List<T>(); } public ImmutableList<T> Add(T

What are disadvantages to using immutable state in React?

回眸只為那壹抹淺笑 提交于 2019-11-28 06:51:13
I have built my first React application with stateful stores the "normal" way, and now I am looking into using an immutable global state like used in the Este starterkit. The state of all stores is kept together in a single immutable data structure Components have no state but access data in their render() based on a store getter function Stores are also stateless but mutate the global application state for their domain using a cursor. The top level app component listens for state changes, and re-renders the whole component tree. Components are implemented as "pure" meaning they use

safe publication and the advantage of being immutable vs. effectively immutable

徘徊边缘 提交于 2019-11-28 06:50:53
I'm re-reading Java Concurrency In Practice, and I'm not sure I fully understand the chapter about immutability and safe publication. What the book says is: Immutable objects can be used safely by any thread without additional synchronization, even when synchronization is not used to publish them. What I don't understand is, why would anyone ( interested in making his code correct ) publish some reference unsafely? If the object is immutable, and it's published unsafely, I understand that any other thread obtaining a reference to the object would see its correct state, because of the

Is it possible to create mutable versions of objects read in from a plist

*爱你&永不变心* 提交于 2019-11-28 06:24:06
问题 I'm reading into my application a plist which at the top level is an array. It's simple enough to make sure the array starts as mutable self.plistData = [[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myDataSource" ofType:@"plist"]] mutableCopy]; Within each element of the array is another array, each of those contains a dictionary. Based on a tablecell selection I'm changing attributes for the dictionary at the selected index: [self.cellDescriptors[indexPath

Are strings in Ruby mutable? [duplicate]

寵の児 提交于 2019-11-28 06:15:17
This question already has an answer here: Are strings mutable in Ruby? 2 answers Consider the following code: $ irb > s = "asd" > s.object_id # prints 2171223360 > s[0] = ?z # s is now "zsd" > s.object_id # prints 2171223360 (same as before) > s += "hello" # s is now "zsdhello" > s.object_id # prints 2171224560 (now it's different) Seems like individual characters can be changed w/o creating a new string. However appending to the string apparently creates a new string. Are strings in Ruby mutable? Yes, strings in Ruby, unlike in Python, are mutable. s += "hello" is not appending "hello" to s -

C# and immutability and readonly fields… a lie?

旧城冷巷雨未停 提交于 2019-11-28 05:41:19
I have found that People claim that using all readonly fields in a class does not necessarily make that class's instance immutable because there are "ways" to change the readonly field values even after initialization (construction). How? What ways? So my question is when can we really have a "real" immutable object in C#, that I can safely use in threading? Also do anonymous types create immutable objects? And some say LINQ uses immutable objecst internally. How exactly? Eric Lippert You've asked like five questions in there. I'll answer the first one: Having all readonly fields in a class

How to implement “__iadd__()” for an immutable type?

我的梦境 提交于 2019-11-28 05:33:06
问题 I would like to subclass an immutable type or implement one of my own which behaves like an int does as shown in the following console session: >>> i=42 >>> id(i) 10021708 >>> i.__iadd__(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'int' object has no attribute '__iadd__' >>> i += 1 >>> i 43 >>> id(i) 10021696 Not surprisingly, int objects have no __iadd__() method, yet applying += to one doesn't result in an error, instead it apparently creates a

What is the “pin” operator for, and are Elixir variables mutable?

被刻印的时光 ゝ 提交于 2019-11-28 05:15:37
Currently trying to understand the "^" operator in Elixir. From the website: The pin operator ^ can be used when there is no interest in rebinding a variable but rather in matching against its value prior to the match: Source - http://elixir-lang.org/getting_started/4.html With this in mind, you can attach a new value to a symbol like so: iex> x = 1 # Outputs "1" iex> x = 2 # Outputs "2" I can also do: iex> x = x + 1 # Outputs "3"! So my first question is; Are Elixir variables mutable? It sure looks like if that's the case... Shouldn't that be possible in a functional programming language? So

Why are immutable objects in hashmaps so effective?

给你一囗甜甜゛ 提交于 2019-11-28 04:54:18
So I read about HashMap . At one point it was noted: "Immutability also allows caching the hashcode of different keys which makes the overall retrieval process very fast and suggest that String and various wrapper classes (e.g., Integer ) provided by Java Collection API are very good HashMap keys." I don't quite understand... why? String#hashCode : private int hash; ... public int hashCode() { int h = hash; if (h == 0 && count > 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; } Since the contents of a

immutable strings vs std::string

北慕城南 提交于 2019-11-28 04:37:21
I've recent been reading about immutable strings Why can't strings be mutable in Java and .NET? and Why .NET String is immutable? as well some stuff about why D chose immutable strings. There seem to be many advantages. trivially thread safe more secure more memory efficient in most use cases. cheap substrings (tokenizing and slicing) Not to mention most new languages have immutable strings, D2.0, Java, C#, Python, etc. Would C++ benefit from immutable strings? Is it possible to implement an immutable string class in c++ (or c++0x) that would have all of these advantages? update: There are two