immutability

Immutable class in Eiffel

久未见 提交于 2019-12-07 04:03:50
问题 I'm trying to make an immutable POINT class in Eiffel. Is the code below defines one? The {NONE} accessibility for the x and y fields is enough for it? Can I write something to the class invariant like x = x' , or how else can I achieve immutability? class POINT create make feature {NONE} x: DOUBLE y: DOUBLE feature make (x_: DOUBLE; y_: DOUBLE) do x := x_ y := y_ ensure set: x = x_ and y = y_ end feature --accessors get_x: DOUBLE do Result := x ensure Result = x end end 回答1: Eiffel does not

How to convert generator or iterator to list recursively

坚强是说给别人听的谎言 提交于 2019-12-07 01:32:42
问题 I want to convert generator or iterator to list recursively. I wrote a code in below, but it looks naive and ugly, and may be dropped case in doctest. Q1. Help me good version. Q2. How to specify object is immutable or not? import itertools def isiterable(datum): return hasattr(datum, '__iter__') def issubscriptable(datum): return hasattr(datum, "__getitem__") def eagerlize(obj): """ Convert generator or iterator to list recursively. return a eagalized object of given obj. This works but,

Inheriting behaviours for set and frozenset seem to differ

牧云@^-^@ 提交于 2019-12-07 00:08:59
问题 Can someone explain the following behaviour: class derivedset1(frozenset): def __new__(cls,*args): return frozenset.__new__(cls,args) class derivedset2(set): def __new__(cls,*args): return set.__new__(cls,args) a=derivedset1('item1','item2') # WORKS b=derivedset2('item1','item2') # DOESN'T WORK Traceback (most recent call last): File "inheriting-behaviours.py", line 12, in <module> b=derivedset2('item1','item2') # DOESN'T WORK TypeError: derivedset2 expected at most 1 arguments, got 2 This is

What's the most concise way to create an immutable class in C#?

拟墨画扇 提交于 2019-12-06 23:39:55
问题 I find myself having to create a lot of immutable classes and I'd like to find a way to do it with no redundant information. I can't use an anonymous type because I need to return these classes from methods. I want intellisense support, so I'd prefer not to use Dictionaries, dynamic or anything like that. I also want well-named properties, which rules out Tuple<>. So far, some patterns I've tried: // inherit Tuple<>. This has the added benefit of giving you Equals() and GetHashCode() public

Is a constexpr more “constant” than const?

柔情痞子 提交于 2019-12-06 19:46:15
问题 The C++ Programming Language Fourth Edition - Bjarne Stroustrup: (emphasis mine) 2.2.3. Constants In a few places, constant expressions are required by language rules (e.g., array bounds (§2.2.5, §7.3), case labels (§2.2.4, §9.4.2), some template arguments (§25.2), and constants declared using constexpr). In other cases, compile-time evaluation is important for performance. Independently of performance issues, the notion of immutability (of an object with an unchangeable state) is an

Why is Font immutable?

我们两清 提交于 2019-12-06 19:06:35
问题 Font being immutable distresses both the programmer and the GC, because you need to create a new instance every time. Why is Font an immutable reference type then? 回答1: It simplifies the usage from the render system. If the framework were to allow Font to be mutable, it would need to detect changes, and rework how it's rendering happens on a regular basis. Since Font creates a native resource, keeping this immutable prevents the system from worrying about having to recreate handles internally

Normalized and immutable data model

≯℡__Kan透↙ 提交于 2019-12-06 18:26:13
问题 How does Haskell solve the "normalized immutable data structure" problem? For example, let's consider a data structure representing ex girlfriends/boyfriends: data Man = Man {name ::String, exes::[Woman]} data Woman = Woman {name :: String, exes::[Man]} What happens if a Woman changes her name and she had been with 13 man? Then all the 13 man should be "updated" (in the Haskell sense) too? Some kind of normalization is needed to avoid these "updates". This is a very simple example, but

ReadOnlyCollection vs Liskov - How to correctly model immutable representations of a mutable collection

孤人 提交于 2019-12-06 18:22:01
问题 Liskov-substitution principle requires that subtypes must satisfy the contracts of super-types. In my understanding, this would entail that ReadOnlyCollection<T> violates Liskov. ICollection<T> 's contract exposes Add and Remove operations, but the read only subtype does not satisfy this contract. For example, IList<object> collection = new List<object>(); collection = new System.Collections.ObjectModel.ReadOnlyCollection<object>(collection); collection.Add(new object()); -- not supported

Is there any run-time overhead to readonly?

依然范特西╮ 提交于 2019-12-06 17:04:18
问题 For some reason, I've always assumed that readonly fields have overhead associated with them, which I thought of as the CLR keeping track of whether or not a readonly field has been initialized or not. The overhead here would be some extra memory usage to keep track of the state and a check when assigning a value. Perhaps I assumed this because I didn't know a readonly field could only be initialized inside a constructor or within the field declaration itself and without a run-time check, you

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

元气小坏坏 提交于 2019-12-06 14:29:06
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? I would like for it to remain the same class as before. Return Collections.unmodifiableMap(map) ,