immutability

Make Object Immutable at Runtime [C#]

这一生的挚爱 提交于 2019-12-22 12:01:50
问题 Is there any way (utilizing Reflection I hope) that I can make an instantiated object immutable along with all of its public properties ? I have a class from someone else's codebase (no source available) that I need to utilize and I basically want an exception to be thrown if any piece of code anywhere tries to call a public setter within this class after it has been instantiated. Note: I do not want to create a wrapper object around the class in order to implement this. I am lazy. 回答1: No

Make Object Immutable at Runtime [C#]

半世苍凉 提交于 2019-12-22 12:01:12
问题 Is there any way (utilizing Reflection I hope) that I can make an instantiated object immutable along with all of its public properties ? I have a class from someone else's codebase (no source available) that I need to utilize and I basically want an exception to be thrown if any piece of code anywhere tries to call a public setter within this class after it has been instantiated. Note: I do not want to create a wrapper object around the class in order to implement this. I am lazy. 回答1: No

Create two immutable objects with the same value in Python

这一生的挚爱 提交于 2019-12-22 11:29:58
问题 Is it possible in Python to create two immutable objects with the same value? So that you understand what I mean, here are some examples: >>> a = 13 >>> b = 13 >>> a is b True >>> a = 13 >>> b = 26/2 >>> a is b True >>> a = 13 >>> b = int.__new__(int, 13) >>> a is b True >>> a = 13 >>> b = int("13") >>> a is b True Is it possible to create a and b with the same value but a is b to return False ? Just learning.... :D 回答1: Sure, just choose a value that is too large to be cached: >>> a = 256 >>

Rewriting state in Redux

牧云@^-^@ 提交于 2019-12-22 11:10:19
问题 In redux I understand that state is immutable and when you create new state you are essentially updating the object with what ever new information there is and then totally rewriting the state. Today I had a thought and I am not sure how stupid it is. Is it computationally expensive to keep re-writing the state? I know that this is one of the major paradigms of Redux, but I want to know if this makes sense from a memory and space perspective. 回答1: You are allowed to mutate the state in Redux

Java String Mutability - java.lang.NoSuchFieldException: offset

杀马特。学长 韩版系。学妹 提交于 2019-12-22 10:17:37
问题 I'm new to Java and I saw a Q&A section here with two examples where mutability is removed. Upon testing MutableString.java: import java.lang.reflect.Field; public class MutableString { public static void main(String[] args) { String s = "Immutable"; String t = "Notreally"; mutate(s, t); StdOut.println(t); // strings are interned so this doesn't even print "Immutable" (!) StdOut.println("Immutable"); } // change the first min(|s|, |t|) characters of s to t public static void mutate(String s,

Immutable graph-like structures in Scala

时间秒杀一切 提交于 2019-12-22 09:56:10
问题 Good day! I'm trying to construct immutable graph in Scala 2.9.1. It's given to me with Seq[BO] , where BO can represent one node in graph, and BO.attr_bo: Seq[String] who represents edges to other nodes, given by string name. And I need to construct "resolved" graph, represented by BO with ResolvedBO You can see possible realization here: trait BO { def name: String def attr_bo: Seq[String] } trait ResolvedBO { x: BO => val uni: Universe lazy val r_attr_bo: Seq[BO with ResolvedBO] = attr_bo

Why number are immutable in Javascript?

ⅰ亾dé卋堺 提交于 2019-12-22 08:12:13
问题 I have read the question and answer here: javascript numbers- immutable But it's not enough clear for me why the number (primitive type) are immutable? Just because they create a new reference but not overwrite the value? If on each assignemt is created a new reference var x = 5; x = 1; Would we have 100 times a new reference in the following loop? while (x < 101) { x++; } Is that efficient? I think I am not seeing correctly. 回答1: I'm honestly not quite sure what kind of answer you expect

Critique of immutable classes with circular references design, and better options

耗尽温柔 提交于 2019-12-22 05:33:41
问题 I have a factory class that creates objects with circular references. I'd like them to be immutable (in some sense of the word) too. So I use the following technique, using a closure of sorts: [<AbstractClass>] type Parent() = abstract Children : seq<Child> and Child(parent) = member __.Parent = parent module Factory = let makeParent() = let children = ResizeArray() let parent = { new Parent() with member __.Children = Seq.readonly children } [Child(parent); Child(parent); Child(parent)] |>

Why are properties of an immutable object mutable in Swift?

谁说胖子不能爱 提交于 2019-12-22 05:10:34
问题 In Swift, we denote an immutable variable with let . What I don't understand is why you change their properties. For example: let lbl = UILabel() lbl.textAlignment = .Right() Why can you change textAlignment ? By virtue of mutating the property, haven't we also mutated the variable lbl that was supposed to be constant? 回答1: According to the Swift Programming Language, the properties of constant structs are also constant, but constant classes can have mutable properties. In their words, If you

What are the real advantages of immutable collections?

你离开我真会死。 提交于 2019-12-22 05:09:21
问题 Scala provides immutable collections, such as Set , List , Map . I understand that the immutability has advantages in concurrent programs. However what are exactly the advantages of the immutability in regular data processing? What if I enumerate subsets , permutations and combinations for example? Does the immutable collections have any advantage here? 回答1: What are exactly the advantages of the immutability in regular data processing? Generally speaking, immutable objects are easier/simpler