immutability

A workaround for Python's missing frozen-dict type? [duplicate]

怎甘沉沦 提交于 2019-12-18 19:39:59
问题 This question already has answers here : Immutable dictionary in Python 3: how to make keys(), items(), and values() dictionary views immutable (2 answers) What would a “frozen dict” be? (14 answers) Closed 3 years ago . In Python, when you want to use lists as keys of some dictionary, you can turn them into tuples, which are immutable and hence are hashable. >>> a = {} >>> a[tuple(list_1)] = some_value >>> a[tuple(list_2)] = some_other_value The same happens when you want to use set objects

Can I deserialize to an immutable object using JavascriptSerializer?

不羁岁月 提交于 2019-12-18 17:56:36
问题 using System.Web.Script.Serialization.JavaScriptSerializer can I deserialize into an immutable object somehow? public class Item { public Uri ImageUri { get;private set; } public string Name { get; private set; } public Uri ItemPage { get;private set; } public decimal Retail { get;private set; } public int? Stock { get; private set; } public decimal Price { get; private set; } public Item(Uri imageUri, string name, Uri itemPage, decimal retail, int? stock, decimal price) { ImageUri = imageUri

Can I deserialize to an immutable object using JavascriptSerializer?

南笙酒味 提交于 2019-12-18 17:56:17
问题 using System.Web.Script.Serialization.JavaScriptSerializer can I deserialize into an immutable object somehow? public class Item { public Uri ImageUri { get;private set; } public string Name { get; private set; } public Uri ItemPage { get;private set; } public decimal Retail { get;private set; } public int? Stock { get; private set; } public decimal Price { get; private set; } public Item(Uri imageUri, string name, Uri itemPage, decimal retail, int? stock, decimal price) { ImageUri = imageUri

Undo/Redo with immutable objects

こ雲淡風輕ζ 提交于 2019-12-18 16:48:40
问题 I read the following in an article Immutable objects are particularly handy for implementing certain common idioms such as undo/redo and abortable transactions. Take undo for example. A common technique for implementing undo is to keep a stack of objects that somehow know how to run each command in reverse (the so-called "Command Pattern"). However, figuring out how to run a command in reverse can be tricky. A simpler technique is to maintain a stack of immutable objects representing the

In immutable class why fields are marked as private?

折月煮酒 提交于 2019-12-18 13:36:19
问题 What is the benefit of making fields private while creating an immutable class? I have seen why while creating immutable class, fields are declared as private? but I didn't get understand anything from this post. Can anybody please explain me the same? 回答1: The best way to explain is with an example: public class Immutable { private final char[] state = "Hi Mom".getChars(); public char[] getState() { return state.clone(); } } Here we have a properly encapsulated, immutable class. Nothing can

Spring Webflow DataBinding to immutable objects via a constructor?

﹥>﹥吖頭↗ 提交于 2019-12-18 13:34:36
问题 Is there any way of using an immutable object as a model within a view-state in Spring webflow? I know Spring webflow generally tends towards setters for this kind of thing, but I was wondering if anyone knew of a custom DataBinder or WebDataBinder that could handle binding the data using a constructor? I'm also aware there's this (SPR-1488) JIRA task against the problem, which advocated direct field access. Do people advocate this way of doing things? To me it doesn't quite feel right.

Why is PostgreSQL calling my STABLE/IMMUTABLE function multiple times?

≯℡__Kan透↙ 提交于 2019-12-18 11:48:10
问题 I'm trying to optimise a complex query in PostgreSQL 9.1.2, which calls some functions. These functions are marked STABLE or IMMUTABLE and are called several times with the same arguments in the query. I assumed PostgreSQL would be smart enough to only call them once for each set of inputs - after all, that's the point of STABLE and IMMUTABLE, isn't it? But it appears that the functions are being called multiple times. I wrote a simple function to test this, which confirms it: CREATE OR

In Scala, difference between final val and val [duplicate]

筅森魡賤 提交于 2019-12-18 10:59:16
问题 This question already has answers here : Scala final vs val for concurrency visibility (2 answers) Closed 5 years ago . In Scala, what is the difference between val a = 1 and final val fa = 1 回答1: final members cannot be overridden, say, in a sub-class or trait. Legal: class A { val a = 1 } class B extends A { override val a = 2 } Illegal: class A { final val a = 1 } class B extends A { override val a = 2 } You'll get an error such as this: :9: error: overriding value a in class A of type Int

What's the use of System.String.Copy in .NET?

☆樱花仙子☆ 提交于 2019-12-18 10:58:40
问题 I'm afraid that this is a very silly question, but I must be missing something. Why might one want to use String.Copy(string)? The documentation says the method Creates a new instance of String with the same value as a specified String. Since strings are immutable in .NET, I'm not sure what's the benefit of using this method, as I'd think that string copy = String.Copy(otherString); would for all practical purposes seem to yield the same result as string copy = otherString; That is, except

Why is shared mutability bad?

 ̄綄美尐妖づ 提交于 2019-12-18 10:15:31
问题 I was watching a presentation on Java, and at one point, the lecturer said: "Mutability is OK, sharing is nice, shared mutability is devil's work." What he was referring to is the following piece of code, which he considered an "extremely bad habit": //double the even values and put that into a list. List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 1, 2, 3, 4, 5); List<Integer> doubleOfEven = new ArrayList<>(); numbers.stream() .filter(e -> e % 2 == 0) .map(e -> e * 2) .forEach(e ->