immutability

Types for which “is” keyword may be equivalent to equality operator in Python

£可爱£侵袭症+ 提交于 2019-11-29 11:47:32
For some types in Python, the is operator seems to be equivalent to the == operator. For example: >>> 1 is 1 True >>> "a spoon" is "a spoon" True >>> (1 == 1) is (2 == 2) True However, this is not always the case: >>> [] == [] True >>> [] is [] False This makes sense for mutable types such as lists. However, immutable types such as tuples seem to display the same behavior: >>> (1, 2) == (1, 2) True >>> (1, 2) is (1, 2) False This raises several questions: Is the == / is equivalence related to immutability? Are the behaviors above specified, or an implementation detail? Most importantly (and

FindBugs : real threat behind EI_EXPOSE_REP

我的未来我决定 提交于 2019-11-29 11:46:34
FindBugs raises a bug called EI_EXPOSE_REP with the following description : EI: May expose internal representation by returning reference to mutable object Returning a reference to a mutable object value stored in one of the object's fields exposes the internal representation of the object. If instances are accessed by untrusted code, and unchecked changes to the mutable object would compromise security or other important properties, you will need to do something different. Returning a new copy of the object is better approach in many situations. Several questions on SO ( 1 , 2 and 3 ) have

Java final keyword for variables

谁都会走 提交于 2019-11-29 11:26:41
问题 How does the final keyword not make a variable immutable? Wikipedia says it doesn't. 回答1: In Java, the term final refers to references while immutable refers to objects. Assigning the final modifier to a reference means it cannot change to point to another object, but the object itself can be modified if it is mutable. For example: final ArrayList<String> arr = new ArrayList<String>(); arr.add("hello"); // OK, the object to which arr points is mutated arr = null; // Not OK, the reference is

NSString immutable allows to change its values?

天大地大妈咪最大 提交于 2019-11-29 10:52:51
The code below compiles and runs, BUT according to all iPhone development books and Apple documentation it shouldnt! Can someone please explain to me how come immutable NSString allows to change its values after it has been set? I thought I had to use NSMuttableString to change context of the same string variable? I am using SDK 3.1. NSString *test =[[NSString alloc] initWithString:@"TEST"]; [test release]; test = @"TEST2"; Perhaps the following example will add upon the replies from Mark and Niels and help clarify things. Immutable strings // Setup two variables to point to the same string

Immutable class in java

空扰寡人 提交于 2019-11-29 10:19:36
I made my class immutable by following all java standards A. Defined class as final B. declared all fields as private and final C. No setter method D. No method changes the state of object E. declared all method as final F. Safer/defencieve copying of collection/ non mutable object fields. These are the priliminary checkpoints I made when desigining immutable class. But one question left, my object can still be modified by java reflection, am I right? Or is there any point I missed in the class? Thanks in advance. There's no hiding from reflection - even immutable classes are not immune. There

Specifying [readonly] property values [via ctor args] when instantiating [immutable] objects with AutoFixture

风格不统一 提交于 2019-11-29 10:19:30
My test requires that I set the Response property on an immutable Rsvp object (see below) to a specific value. public class Rsvp { public string Response { get; private set; } public Rsvp(string response) { Response = response; } } I initially tried to do this using Build<Rsvp>().With(x => x.Rsvp, "Attending") , but realized this only supports writable properties. I replaced that with Build<Rsvp>().FromFactory(new Rsvp("Attending")) . This works, but is cumbersome for more complex objects where it doesn't matter what some of the properties are. For instance, if the Rsvp object had a

When does `modify` copy the vector?

感情迁移 提交于 2019-11-29 09:21:51
From https://hackage.haskell.org/package/vector-0.12.0.1/docs/Data-Vector.html#v:modify Apply a destructive operation to a vector. The operation will be performed in place if it is safe to do so and will modify a copy of the vector otherwise. This sounds like it can have drastically different performance characteristics depending on whether it is deemed "safe" to modify the vector in place. This motivates the questions... When will the modify be performed in place, and when will the vector be copied? Is there some way to ensure, by use of the type-system for example, that it will be modified

Functional Data Structures in Java

不想你离开。 提交于 2019-11-29 08:39:23
问题 Does the Java standard library have any functional data structures, like immutable Sets, Lists, etc., with functional update? 回答1: Functional java has Sets, Lists and more interesting abstractions. 回答2: Have a look at the pcollections project: PCollections serves as a persistent and immutable analogue of the Java Collections Framework. This includes efficient, thread-safe, generic, immutable, and persistent stacks, maps, vectors, sets, and bags, compatible with their Java Collections

How to serialize / deserialize immutable list type in c#

时光总嘲笑我的痴心妄想 提交于 2019-11-29 07:16:12
If I have a class defined [DataContract()] class MyObject { [DataMember()] ImmutableList<string> Strings { get; private set} } The ImmutableList<T> type comes from the immutables library https://www.nuget.org/packages/Microsoft.Bcl.Immutable . Note that the class ImmutableList does not have a default constructor or a mutable Add method. Adding things to the list take the form. myList = myList.Add("new string"); Can I add some custom support to the .NET serialization mechanism to support this type and show it how to deserialize it? Currently the collection is just skipped on deserialization

How can I instantiate immutable mutually recursive objects?

▼魔方 西西 提交于 2019-11-29 06:44:06
问题 I have an immutable recursive type: public sealed class Foo { private readonly object something; private readonly Foo other; // might be null public Foo(object something, Foo other) { this.something = something; this.other = other; } public object Something { get { return something; } } public Foo Other { get { return other; } } } I need to instantiate two objects of this type that refer to each other, i.e. a.Other == b && b.Other == a . I don't want to abandon the immutability invariants