immutability

Is there any way to prevent replacement of JavaScript object properties?

狂风中的少年 提交于 2019-11-29 01:45:40
I would like to make an object's structure immutable, preventing its properties from being subsequently replaced. The properties need to be readable, however. Is this possible? I'm sure there are no language features (along the lines of final in Java and readonly in C#) to support this but wondered whether there might be another mechanism for achieving the same result? I'm looking for something along these lines: var o = { a: "a", f: function () { return "b"; } }; var p = o.a; // OK o.a = "b"; // Error var q = o.f(); // OK o.f = function () { // Error return "c"; }; ECMAScript 5 will have seal

Check for mutability in Python?

不问归期 提交于 2019-11-29 01:39:03
问题 Consider this code: a = {...} # a is an dict with arbitrary contents b = a.copy() What role does mutability play in the keys and values of the dicts? How do I ensure changes to keys or values of one dict are not reflected in the other? How does this relate to the hashable constraint of the dict keys? Are there any differences in behaviour between Python 2.x and Python 3.x? How do I check if a type is mutable in Python? 回答1: 1) Keys must not be mutable, unless you have a user-defined class

What is the most efficient implementation of arrays with functional updates?

一个人想着一个人 提交于 2019-11-29 01:03:24
问题 I need an array-like data structure with the fastest possible functional update. I've seen a few different implementation of flexible arrays that provide me with this property (Braun, Random Access Lists) but I'm wondering if there is an implementation that is specifically optimized for the case when we are not interested in append or prepend - just updates. 回答1: Jean-Cristophe Filliâtre has a very nice implementation of persistent arrays, that is described in the paper linked at the same

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

你。 提交于 2019-11-29 01:03:17
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 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 function. Even though this is still imperative (the object is mutated), it fits reasonably well with the

How can I create a new instance of ImmutableDictionary?

旧城冷巷雨未停 提交于 2019-11-29 00:57:20
I would like to write something like this: var d = new ImmutableDictionary<string, int> { { "a", 1 }, { "b", 2 } }; (using ImmutableDictionary from System.Collections.Immutable ). It seems like a straightforward usage as I am declaring all the values upfront -- no mutation there. But this gives me error: The type ' System.Collections.Immutable.ImmutableDictionary<TKey,TValue> ' has no constructors defined How I am supposed to create a new immutable dictionary with static content? Dirk You can't create immutable collection with a collection initializer because the compiler translates them into

what is “failure atomicity” used by J bloch and how its beneficial in terms of immutable object?

天大地大妈咪最大 提交于 2019-11-29 00:36:12
问题 just came across below statement as benefit of immutable object Immutable object always have “failure atomicity” (a term used by Joshua Bloch) : if an immutable object throws an exception, it’s never left in an undesirable or indeterminate state. can any one explain it in more detail and why is it so? 回答1: Bloch's "Failure atomicity" means that if a method threw an exception, the object should still be usable afterwards. Generally, the object should be in the same state as it was before

Why does Microsoft advise against readonly fields with mutable values?

China☆狼群 提交于 2019-11-29 00:21:37
问题 In the Design Guidelines for Developing Class Libraries, Microsoft say: Do not assign instances of mutable types to read-only fields. The objects created using a mutable type can be modified after they are created. For example, arrays and most collections are mutable types while Int32, Uri, and String are immutable types. For fields that hold a mutable reference type, the read-only modifier prevents the field value from being overwritten but does not protect the mutable type from modification

What is the difference between immutable and const variables in Rust?

两盒软妹~` 提交于 2019-11-28 23:41:48
问题 I learned that if a variable is not explicitly declared mutable using mut , it becomes immutable (it cannot be changed after declaration). Then why do we have the const keyword in Rust? Aren't they same? If not, how do they differ? 回答1: const , in Rust, is short for constant and is related to compile-time evaluation . It shows up: when declaring constants: const FOO: usize = 3; when declaring compile-time evaluable functions: const fn foo() -> &'static str These kinds of values can be used as

What is the difference between const and immutable in D?

♀尐吖头ヾ 提交于 2019-11-28 23:10:33
What is the difference between the const and immutable type qualifiers in D? 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 could mutate it and therefore change what c sees. But if you have immutable C c = foo(); then you know that

Immutable type and property in C#

半城伤御伤魂 提交于 2019-11-28 22:55:12
What is meant by immutable type and immutable property in C# ? can you give simple example? fretje An immutable type is a type of which its properties can only be set at initialization. Once an object is created, nothing can be changed anymore. An immutable property is simply a read-only property. In the following example, ImmutableType is an immutable type with one property Test . Test is a read-only property. It can only be set at construction. class ImmutableType { private readonly string _test; public string Test { get { return _test; } } public ImmutableType(string test) { _test = test; }