immutability

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

安稳与你 提交于 2019-11-27 16:12:50
问题 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"

Why is it possible to implement Read on an immutable reference to File?

拜拜、爱过 提交于 2019-11-27 16:06:36
If you check out the docs for Read , most of the methods accept a &mut self . This makes sense, as reading from something usually updates an internal offset so the next read returns different data. However, this compiles: use std::io::Read; use std::fs::File; fn main() { let file = File::open("/etc/hosts").unwrap(); let vec = &mut Vec::new(); (&file).read_to_end(vec).unwrap(); println!("{:?}", vec); } The file isn't mutable, but the data is certainly being read in. This seems incorrect to me. It was pointed out that there is an impl<'a> Read for &'a File , but the fact that an immutable

How can I create a new instance of ImmutableDictionary?

江枫思渺然 提交于 2019-11-27 15:28:21
问题 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? 回答1: You can

In Rust, what's the difference between “shadowing” and “mutability”?

不问归期 提交于 2019-11-27 15:16:59
In Chapter 3 of the Rust Book , Variables and Mutability , we go through a couple iterations on this theme in order to demonstrate the default, immutable behavior of variables in Rust: fn main() { let x = 5; println!("The value of x is {}", x); x = 6; println!("The value of x is {}", x); } Which outputs: error[E0384]: cannot assign twice to immutable variable `x` --> src/main.rs:4:5 | 2 | let x = 5; | - | | | first assignment to `x` | help: make this binding mutable: `mut x` 3 | println!("The value of x is {}", x); 4 | x = 6; | ^^^^^ cannot assign twice to immutable variable However, because

Scala - initialization order of vals

允我心安 提交于 2019-11-27 15:09:19
I have this piece of code that loads Properties from a file: class Config { val properties: Properties = { val p = new Properties() p.load(Thread.currentThread().getContextClassLoader.getResourceAsStream("props")) p } val forumId = properties.get("forum_id") } This seems to be working fine. I have tried moving the initialization of properties into another val, loadedProperties , like this: class Config { val properties: Properties = loadedProps val forumId = properties.get("forum_id") private val loadedProps = { val p = new Properties() p.load(Thread.currentThread().getContextClassLoader

Is there a way to Object.freeze() a JavaScript Date?

心不动则不痛 提交于 2019-11-27 14:28:21
问题 According to MDN Object.freeze() documentation: The Object.freeze() method freezes an object: that is, prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their enumerability, configurability, or writability, from being changed. In essence the object is made effectively immutable. The method returns the object being frozen. I was expecting that calling freeze on a date would prevent changes to that date, but it

Are strings mutable in Ruby?

送分小仙女□ 提交于 2019-11-27 14:17:20
Are Strings mutable in Ruby? According to the documentation doing str = "hello" str = str + " world" creates a new string object with the value "hello world" but when we do str = "hello" str << " world" It does not mention that it creates a new object, so does it mutate the str object, which will now have the value "hello world" ? Dogbert Yes, << mutates the same object, and + creates a new one. Demonstration: irb(main):011:0> str = "hello" => "hello" irb(main):012:0> str.object_id => 22269036 irb(main):013:0> str << " world" => "hello world" irb(main):014:0> str.object_id => 22269036 irb(main

Does Haskell have variables?

风流意气都作罢 提交于 2019-11-27 12:39:42
I've frequently heard claims that Haskell doesn't have variables; in particular, this answer claims that it doesn't, and it was upvoted at least nine times and accepted. So does it have variables or not, and why? This question also appears to apply ML, F#, OCaml, Erlang, Oz, Lava, and all SSA intermediate languages. Don Stewart Haskell has immutable variables (variables in the math sense) by default: foo x y = x + y * 2 By default variables are not mutable cells . Haskell also has mutable cells though, but you enable them explicitly: > import Data.IORef (newIORef, readIORef, writeIORef) > v <-

Does using public readonly fields for immutable structs work?

可紊 提交于 2019-11-27 11:17:51
Is this a proper way to declare immutable structs? public struct Pair { public readonly int x; public readonly int y; // Constructor and stuff } I can't think of why this would run into problems, but I just wanted to ask to make sure. In this example, I used ints. What if I used a class instead, but that class is also immutable, like so? That should work fine too, right? public struct Pair { public readonly (immutableClass) x; public readonly (immutableClass) y; // Constructor and stuff } (Aside: I understand that using Properties is more generalizable and allows changing, but this struct is

What's the difference between Collections.unmodifiableSet() and ImmutableSet of Guava?

折月煮酒 提交于 2019-11-27 10:59:30
问题 JavaDoc of ImmutableSet says: Unlike Collections.unmodifiableSet , which is a view of a separate collection that can still change, an instance of this class contains its own private data and will never change. This class is convenient for public static final sets ("constant sets") and also lets you easily make a "defensive copy" of a set provided to your class by a caller. But the ImmutableSet still stores reference of elements, I couldn't figure out the difference to Collections