immutability

Read only two-dimensional array in C#

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 07:28:00
问题 Is there any established way of returning a read-only 2-d array in C#? I know ReadOnlyCollection is the right thing to use for a 1-d array, and am happy to write my own wrapper class that implements a this[] {get} . But I don't want to reinvent the wheel if this wheel already exists. 回答1: There's only one way to simulate this. You need to create your own class, with a private array. The most similar implementation of an array is an indexer: Using indexers Indexers (C# programming guide) 10.8

Why isn’t ReadOnlyDictionary thread-safe?

自古美人都是妖i 提交于 2019-12-23 07:19:26
问题 I’m looking for a readonly-dictionary to be accessed from multiple threads. While ConcurrentDictionary exposes such capabilities, I don’t want to have the overhead and the strange API. .Net 4.5 while providing such a class, the documentation states that only static calls are safe. I wonder why? 回答1: ReadOnlyDictionary is just a wrapper around any other dictionary. As such, it's only as thread-safe as the underlying dictionary. In particular, if there's a thread modifying the underlying

Defects of Immutable collections of Guava?

这一生的挚爱 提交于 2019-12-23 05:26:35
问题 I am not sure the defects of Immutable collections I understand is correct, so I list them in this answer. Hope someone corrects me here. a): Comparing to Collections.unmodifiableXXX(), ImmutableXXX.copyOf() loses the source collection feature . For example, when a linkedList is put into ImmutableList.copyOf(), the ImmutableList is not linked anymore. Same as Tree based collection. b): People think Collections.unmodifiableXXX just uses same reference of source collection, so once the source

Is this redux reducer OK

让人想犯罪 __ 提交于 2019-12-23 04:38:31
问题 Is this reducer OK: function someReducer(state = initialState, action) { if (action.type === SOME_ACTION) { const newState = Object.assign( {}, state ); // ... // doing whatever I want with newState // ... return newState; } return state; } and if is OK, why we need all those immutable libraries to complicate our lives. p.s Just trying to comprehend Redux and immutability 回答1: export default function (state = initialState, action) { const actions = { SOME_ACTION: () => { return { ...state } }

How can I assign final variables of the base class within a derived class' constructor in Java?

删除回忆录丶 提交于 2019-12-23 04:23:36
问题 I have a base Color class that looks something like this. The class is designed to be immutable, so as a result has final modifiers and no setters: public class Color { public static Color BLACK = new Color(0, 0, 0); public static Color RED = new Color(255, 0, 0); //... public static Color WHITE = new Color(255, 255, 255); protected final int _r; protected final int _g; protected final int _b; public Color(int r, int b, int g) { _r = normalize(r); _g = normalize(g); _b = normalize(b); }

How can I use NHibernate with immutable type like System.Tuple?

时间秒杀一切 提交于 2019-12-22 21:16:29
问题 I have a composite mapping using System.Tuple<int,string> that looks as follows: <composite-element class="System.Tuple`2[[System.Int32, mscorlib],[System.String, mscorlib]], mscorlib"> <property name="Item1" column="DBColumn1"/> <property name="Item2" column="DBColumn2"/> </composite-element> I try messing around with BytecodeProvider , IObjectsFactory , ReflectionOptimizer and whatnot, but I can't get NHibernate to load my Tuple properly (whatever I do, NHibernate insists on creating the

How to write a test-friendly immutable value class?

六月ゝ 毕业季﹏ 提交于 2019-12-22 19:23:14
问题 I marked an immutable data model class as final to make sure the only way to change its values is to create a new instance. (Unfortunately, the fields cannot be final because they needs to be populated by Hibernate.) This worked well until I wanted to check another class throws the correct exception when called with an invalid instance of the model. The constructor of the model validates the arguments so reflection must be used to set the fields. This is extremely clumsy since the model have

Is there a convention that objects created using the Builder pattern be immutable?

北城以北 提交于 2019-12-22 18:44:15
问题 According to the book Design Patterns: Elements of Reusable Object-Oriented Software,: The Builder Pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. In general the Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object. With the

Java: how to make a private field Map immutable within a class?

孤者浪人 提交于 2019-12-22 12:17:14
问题 public class Hi { private final Map<String, String> map; public Map<String, String> getMap() { return map; } } I have this Hi class, and I want map to be immutable. I also need a getter. Currently, another class can modify the map from the getter. I would like to return a copy of the map to solve this problem, but Map is an interface, so does that mean I have to make the getter call: return new HashMap<String,String>(map); Is there another way to do it without forcing the map to be a hashmap?

Make Object Immutable at Runtime [C#]

依然范特西╮ 提交于 2019-12-22 12:03:07
问题 Is there any way (utilizing Reflection I hope) that I can make an instantiated object immutable along with all of its public properties ? I have a class from someone else's codebase (no source available) that I need to utilize and I basically want an exception to be thrown if any piece of code anywhere tries to call a public setter within this class after it has been instantiated. Note: I do not want to create a wrapper object around the class in order to implement this. I am lazy. 回答1: No