immutability

What does it mean for a collection to be final in Java? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-03 11:16:11
This question already has an answer here: Why can final object be modified? 7 answers What does it mean for a collection to be declared final in Java? Is it that no more elements can be added to it? Is it that the elements already there cannot be changed? Is it something else? No. It simply means that the reference cannot be changed. final List list = new LinkedList(); .... list.add(someObject); //okay list.remove(someObject); //okay list = new LinkedList(); //not okay list = refToSomeOtherList; //not okay You are getting confused between final and immutable Objects. final --> You cannot

Efficiently “modifying” an ImmutableMap

送分小仙女□ 提交于 2019-12-03 10:50:56
We're currently using Guava for its immutable collections but I was surprised to find that their maps don't have methods to easily create new maps with minor modifications. And on top of that, their builder doesn't allow assigning new values to keys, or removing keys. So if I wanted to modify just one value, here's what I would like to be able to do: ImmutableMap<Guid, ImmutableMap<String, Integer>> originalMap = /* get the map */; ImmutableMap<Guid, ImmutableMap<String, Integer>> modifiedMap = originalMap.cloneAndPut(key, value); Here's what it looks like Guava are expecting me to do:

List.empty vs. List() vs. new List()

别来无恙 提交于 2019-12-03 10:42:47
问题 What the difference between List.empty , List() and new List() ? When should I use which? 回答1: First of all, new List() won't work, since the List class is abstract. The other two options are defined as follows in the List object: override def empty[A]: List[A] = Nil override def apply[A](xs: A*): List[A] = xs.toList I.e., they're essentially equivalent, so it's mostly a matter of style. I prefer to use empty because I find it clearer, and it cuts down on parentheses. 回答2: From the source

Hashing an immutable dictionary in Python

怎甘沉沦 提交于 2019-12-03 10:30:51
Short version: What's the best hashing algorithm for a multiset implemented as a dictionary of unordered items? I'm trying to hash an immutable multiset (which is a bag or multiset in other languages: like a mathematical set except that it can hold more than one of each element) implemented as a dictionary. I've created a subclass of the standard library class collections.Counter , similar to the advice here: Python hashable dicts , which recommends a hash function like so: class FrozenCounter(collections.Counter): # ... def __hash__(self): return hash(tuple(sorted(self.items()))) Creating the

Are .NET enum types actually mutable value types?

谁说我不能喝 提交于 2019-12-03 10:06:59
Looking, with reflection, at the fields of an enum type, I noticed to my surprise that the "backing" instance field that holds the actual value of a particular instance of the enum is not private , as I would have thought, but public . And it was not readonly either. ( IsPublic true, IsInitOnly false.) Many people consider "mutable" value types in the .NET type system "evil", so why are the enum types (as created from C# code for example) just that? Now, as it turns out, the C# compiler has some kind of magic that denies the existence of the public instance field (but see below), but in e.g.

Immutable Object in Objective-C: Big init method?

泪湿孤枕 提交于 2019-12-03 09:49:54
I want to have an Object with immutable fields in Objective-C. In C#, I would use Properties with private setters and a big constructor. What would I use in Objective-C? Using @property doesn't seem to allow me to declare the setter as private. Using initWithData: (NSString*) something createDate: (NSDate*) date userID: (long) uid seems overly verbose if I have more than 4 properties to set. Would I declare the getters in the .h file and the setters only in .m? I need to use retain or copy on something and date (by the way: which of these two should I use?), so I need some code in the setter.

Best way to separate read and write concerns using interfaces?

邮差的信 提交于 2019-12-03 09:45:42
问题 Lately I've been realizing the benefit of (some would argue overuse of) immutable objects to cut down dramatically on read-write dependency issues in my object model and their resulting conditions and side-effects, to ultimately make the code simpler to manage (kind of functional-programming-esque). This practice has led me to create read-only objects that are provided values at creation/construction time and then to make available only public getters for external callers to access the

What is the theory behind mutable and immutable types?

浪尽此生 提交于 2019-12-03 09:35:13
问题 One of the things that I admire about Python is its distinction between mutable and immutable types. Having spent a while programming in c before coming to Python, I was astonished at how easily Python does away with all the complexities of pointer dereferencing that drive me mad in c. In Python everything just works the way I expect, and I quickly realized that the mutable/immutable distinction plays an important part in that. There are still a few wrinkles, of course (mutable function

What are the advantages of built-in immutability of F# over C#?

限于喜欢 提交于 2019-12-03 09:34:47
I heard F# has native support for immutability but what about it that can not be replicated in C#? What do you get by an F# immutable data that you don't get from a C# immutable data? Also in F#, is there no way to create mutable data? Is everything immutable? If you use both C# and F# in an application, can you change the immutable F# data in C#? Or do you just have to create new C# types that uses the immutable F# data and replaces the references that points to those data? The way F# works makes it easier to work with immutable data but there's nothing special that can't be done in C# in

Can immutable be a memory hog?

↘锁芯ラ 提交于 2019-12-03 09:30:17
Let's say we have a memory-intensive class like an Image , with chainable methods like Resize() and ConvertTo() . If this class is immutable, won't it take a huge amount of memory when I start doing things like i.Resize(500, 800).Rotate(90).ConvertTo(Gif) , compared to a mutable one which modifies itself? How to handle a situation like this in a functional language? If this class is immutable, won't it take a huge amount of memory? Typically your memory requirements for that single object might double, because you might have an "old copy" and a "new copy" live at once. So you can view this