immutability

Immutable object in collections (C++ and Qt)

徘徊边缘 提交于 2019-12-11 01:04:18
问题 I am stuck with using immutable objects with collections. Let assume I have the following class : //in .h class Data { public: Data(const DataObj& data); Data(const Data&); const DataObj& getDataObj() const; private: const DataObj _data; //actually several objects or simple type } inline const DataObj& Data::getDataObj() const {return _data}; //in .c Data(const DataObj& data) : _data(data){}; Data(const Data& original) : _data(original._data){} The issue is that when I want to use collections

how to access complex data structures in Scala while preserving immutability?

荒凉一梦 提交于 2019-12-11 00:34:20
问题 Calling expert Scala developers! Let's say you have a large object representing a writable data store. Are you comfortable with this common Java-like approach: val complexModel = new ComplexModel() complexModel.modify() complexModel.access(...) Or do you prefer: val newComplexModel = complexModel.withADifference newComplexModel.access(...) If you prefer that, and you have a client accessing the model, how is the client going to know to point to newComplexModel rather than complexModel? From

Does an immutable list that overloads '+' makes sense?

微笑、不失礼 提交于 2019-12-10 23:19:51
问题 It certainly does not break from the standard practice of the .NET framework. When I see a a + b I always assume something new will be created. static void Main(string[] args) { var list = BuildList(ImmutableList<int>.Empty); var sum = (list + 500).Sum(); Console.WriteLine(sum); Console.ReadLine(); } static ImmutableList<int> BuildList(ImmutableList<int> list) { if (list.Count < 1000) { return BuildList(list + list.Count); } return list; } Update See Jon Skeet's post on what to name methods

Is it possible to serialize/deserialize immutable types with protobuf-net on Windows Phone 7/8?

让人想犯罪 __ 提交于 2019-12-10 20:00:00
问题 Is it possible to serialize/deserialize types with protobuf-net on Windows Phone 7/8? I've tried the code below, it seems Constructor skipping isn't supported (i.e. UseConstructor = false) so I created a parameterless constructor but the deserialization fails with "Attempt to access the method failed: Wp7Tests.ImmutablePoint.set_X(System.Double)" public class ImmutablePoint { public double X { get; private set; } public double Y { get; private set; } public ImmutablePoint() {} public

How do I declare an immutable variable (value) in C#? [duplicate]

若如初见. 提交于 2019-12-10 19:11:52
问题 This question already has answers here : variable that can't be modified (9 answers) Closed 2 years ago . In Scala I can write (and it will mean exactly the same thing it means in C#) var v = 1; v = 2; but can't write (well, of course I can write but can't compile actually though the syntax is correct) val v = 1; v = 2; Semicolons are not necessary but can be voluntarily used in Scala so I've decided to include them to let the code correspond C# more closely. val means an immutable value, a

How to interpret immutable references to mutable types in Rust?

五迷三道 提交于 2019-12-10 18:48:07
问题 It seems that I cannot mutate anything if there is any immutable reference in my chain of dereferencing. A sample: fn main() { let mut x = 42; let y: &mut i32 = &mut x; // first layer let z: &&mut i32 = &y; // second layer **z = 100; // Attempt to change `x`, gives compiler error. println!("Value is: {}", z); } I'm getting the compiler error: error[E0594]: cannot assign to `**z` which is behind a `&` reference --> src/main.rs:5:5 | 4 | let z: &&mut i32 = &y; // second layer | -- help:

Mutable values in an object

ぐ巨炮叔叔 提交于 2019-12-10 18:16:13
问题 In Scala, if I'm planning on having a mutable attribute (e.g. a bag of numbers) of an object, when is it appropriate to Create a var and use an immutable data structure? Create a val and use a mutable data structure? I'm going to throw a guess out that you'd want to use #2 for threaded applications? Are some of the collections thread-safe? How about in general? (Or does it not really matter?) 回答1: Between your choices 1 and 2, it doesn't matter - mutable is mutable, and if you read or modify

Can i make different variables of same values point to diffrent objects?

只谈情不闲聊 提交于 2019-12-10 17:48:50
问题 I think this question might already have an answer here on SO, but I can't seem to find it. Please mark it as duplicate if you find an answer. I am not asking why, but how can I not let this happen? I want j to have a diffrent id than i . Say when i do, >>> i = 6 >>> j = 6 >>> id(i) 10919584 >>> id(j) 10919584 #I don't want this, I want j to point to a different object So, I get what happens in the code above(Or at least I think I do), but my question is how can I prevent it? I am asking it

Why do tuples in Python work with reversed but do not have __reversed__?

僤鯓⒐⒋嵵緔 提交于 2019-12-10 16:55:00
问题 In discussion of this answer we realized that tuples do not have a __reversed__ method. My guess was that creating the iterator would require mutating the tuple. And yet tuples play fine with reversed . Why can't the approach used for reversed be made to work for __reversed__ as well? >>> foo = range(3) >>> foo [0, 1, 2] >>> list(foo.__reversed__()) [2, 1, 0] >>> foo [0, 1, 2] >>> bar = (0, 1, 2) >>> list(bar.__reversed__()) Traceback (most recent call last): File "<stdin>", line 1, in

Immutable list in Python

三世轮回 提交于 2019-12-10 16:53:44
问题 I'm trying to make a list which is used throughout the application immutable. I thought wrapping this list in a tuple would do the trick, but it seems that tuple(list) doesn't actually wrap, but copies the list elements. >>> a = [1, 2, 3, 4] >>> b = tuple(a) >>> b (1, 2, 3, 4) >>> a[0] = 2 >>> b # was hoping b[0] to be 2 (1, 2, 3, 4) Is there an easy way of creating a list-backed "view" on this list that is immutable (wrt. operations on this view), but reflects any change that happened to the