immutability

Are Immutable objects immune to improper publication?

我的梦境 提交于 2019-12-18 09:19:17
问题 It is an example from JCiP. public class Unsafe { // Unsafe publication public Holder holder; public void initialize() { holder = new Holder(42); } } public class Holder { private int n; public Holder(int n) { this.n = n; } public void assertSanity() { if (n != n) { throw new AssertionError("This statement is false."); } } } On page 34: [15] The problem here is not the Holder class itself, but that the Holder is not properly published. However, Holder can be made immune to improper

Why two individually created immutable objects have same id and mutable objects have different while both refer to same values? [duplicate]

拥有回忆 提交于 2019-12-18 08:46:34
问题 This question already has an answer here : What's with the integer cache maintained by the interpreter? (1 answer) Closed 5 years ago . Two individually created mutable list have different ids. Python SHELL: (mutable) >>> mylist = ['spam', 'eggs'] >>> yourlist = ['spam', 'eggs'] >>> id(mylist), id(yourlist) (49624456, 48910408) While two individually created immutable strings have similar ids. Python SHELL: (immutable) >>> a = 10 >>> b = 10 >>> id(a), id(b) (507099072, 507099072) Is a and b

Angular 2 Components: How to handle circular Inputs and Outputs

落爺英雄遲暮 提交于 2019-12-18 07:45:15
问题 Current Situation: I have a parent and a child component. The parent initializes the child 's data using its @Input . And the child notifies the parent, when the user edited the data using the @Output . And because the data is immutable , the child has to send the data along with that notification. When the parent got notified, it will check if the submitted data is valid, and then will set it (this will propagate the new value to some other child components as well). The Problem: When

FindBugs : real threat behind EI_EXPOSE_REP

删除回忆录丶 提交于 2019-12-18 07:02:43
问题 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

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

与世无争的帅哥 提交于 2019-12-18 06:58:12
问题 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

Immutable class in java

三世轮回 提交于 2019-12-18 05:54: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

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

丶灬走出姿态 提交于 2019-12-18 04:52:28
问题 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

Is using a StringBuilder a right thing to do in F#?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 03:01:59
问题 StringBuiler is a mutable object, F# encourages employing immutability as much as possible. So one should use transformation rather than mutation. Does this apply to StringBuilder when it comes to building a string in F#? Is there an F# immutable alternative to it? If so, is this alternative as efficient? A snippet 回答1: I think that using StringBuilder in F# is perfectly fine - the fact that sb.Append returns the current instance of StringBuilder means that it can be easily used with the fold

What is the difference between const and immutable in D?

耗尽温柔 提交于 2019-12-18 02:00:08
问题 What is the difference between the const and immutable type qualifiers in D? 回答1: Something that is const cannot be mutated via that reference but could be mutated by a mutable reference to the same data. Something that is immutable can't be mutated by any reference to that data. So, if you have const C c = foo(); then you know that you cannot mutate the object referred to by c through c , but other references to the object referred to by c may exist in your code, and if they're mutable, they

How to initialize and “modify” a cyclic persistent data structure in Scala?

会有一股神秘感。 提交于 2019-12-17 23:46:35
问题 I have searched and found some info on this topic but the answers are either confusing or not applicable. I have something like this: class Thing (val name:String, val refs:IndexedSeq[Ref]) class Ref (val name:String, val thing:Thing) Now, I want to say, load in a file, parse it and populate this data structure from it. It being immutable and cyclic, how might one do so? Also, let's say I do get this data structure populated, now I want to modify it, like change rootThing.refs(3).name, how