immutability

Passing a closure that modifies its environment to a function in Rust

≯℡__Kan透↙ 提交于 2020-01-04 07:36:41
问题 I have a closure that captures and modifies its environment. I want to pass this closure to a function that accepts closures: fn main() { let mut integer = 5; let mut closure_variable = || -> i32 { integer += 1; integer }; execute_closure(&mut closure_variable); } fn execute_closure(closure_argument: &mut Fn() -> i32) { let result = closure_argument(); println!("Result of closure: {}", result); } Because the closure modifies its environment, this fails: error[E0525]: expected a closure that

convert a mutable object to an immutable object

狂风中的少年 提交于 2020-01-04 06:36:28
问题 I'd like to make a bean immutable and the traditional approach is to make all fields final and set their values in the constructor. In my opinion this works well, unless there are many fields which need to be calculated interdependently via some complex logic or when multiple services need to take part in setting values. A factory is another approach but I can't come up with a tidy model that avoids too much code. What I'd like to do is create a mutable instance of the bean and once it is

How to Mutate an Array in a Dictionary?

删除回忆录丶 提交于 2020-01-03 16:40:06
问题 I've tried the following in a Playground: var d1 = [String: [String]]() d1["a"] = [String]() var a1 = d1["a"]! a1.append("s1") println(d1) The output is: [a: []] I was hoping for: [a: ["s1"]] What would be the right way to mutate an array in a dictionary? 回答1: In swift, structures are copied by value when they get assigned to a new variable. So, when you assign a1 to the value in the dictionary it actually creates a copy. Here's the line I'm talking about: var a1 = d1["a"]! Once that line

Reassignment, mutation, reference types, and value types

主宰稳场 提交于 2020-01-03 03:09:26
问题 How would you appropriately explain why these two examples differ? // Reassignment let a = 1; let b = a; a = 2; console.log(b); // → 1 // Mutation let myArray = [1, 2, 3]; let ourArray = myArray; ourArray[2] = 10; console.log(myArray); // → [1, 2, 10] In regards to why the variable label behaves differently between the two, many resources claim that it's because of the difference between reference types and value types. But, doesn't that only apply to the differences between the values

Regarding an Immutable class defensive copying

半腔热情 提交于 2020-01-02 07:11:15
问题 I have a query regarding creating a Immutable class. Following are the points which I take in consideration: Make the class final Make all members final, set them explicitly, in a static block, or in the constructor Make all members private No Methods that modify state Be extremely careful to limit access to mutable member components(remember the field may be final but the object can still be mutable. ie private final Date imStillMutable) - See defensive copying or its cousin copy

How to make Scala's immutable collections hold immutable objects

别说谁变了你拦得住时间么 提交于 2020-01-02 03:54:06
问题 I'm evaluating Scala and am having a problem with its immutable collections. I want to make immutable collections, which are completely immutable, right down through all the contained objects, the objects they reference, ad infinitum. Is there a simple way to do this? The code on http://www.finalcog.com/immutable-containers-scala illustrates what I'm trying to achieve, and a nasty work around (ImmutablePoint). The problem with the workaround is that every time I want to change an object I

How deep would you expect the immutability of an immutable list to be?

陌路散爱 提交于 2020-01-02 03:25:21
问题 If you have an immutable list, you expect it to always return a reference to the same object when you ask for, say list.get(0) My question is, would you expect to be able to mutate the object and have the mutation reflected next time you get it from the list? 回答1: It depends on the context. In a general purpose library, all we should assume is that the list is immutable. Changes to the elements in the list would be reflected to all callers, as a direct consequence of returning the same

Is this a valid Java implementation of an immutable class and the Builder pattern?

浪尽此生 提交于 2020-01-02 01:41:13
问题 The Builder implements Cloneable and overrides clone() and instead of copying every field of the builder, the immutable class keeps a private clone of the builder. This makes it easy to return a new builder and create slightly modified copies of an immutable instance. This way I can go MyImmutable i1 = new MyImmutable.Builder().foo(1).bar(2).build(); MyImmutable i2 = i1.builder().foo(3).build(); The Cloneable interface is said to be somewhat broken, but does any of this violate good java

What does it mean for a collection to be final in Java? [duplicate]

喜你入骨 提交于 2020-01-01 04:23:09
问题 This question already has answers here : Why can final object be modified? (7 answers) Closed 5 years ago . What does it mean for a collection to be declared final in Java? Is it that no more elements can be added to it? Is it that the elements already there cannot be changed? Is it something else? 回答1: No. It simply means that the reference cannot be changed. final List list = new LinkedList(); .... list.add(someObject); //okay list.remove(someObject); //okay list = new LinkedList(); //not

Java - Immutable array thread-safety

蹲街弑〆低调 提交于 2020-01-01 03:04:27
问题 I have a question regarding the Java Memory Model. Here is a simple class presenting the problem: public class ImmutableIntArray { private final int[] array; public ImmutableIntArray() { array = new int[10]; for (int i = 0; i < 10; i++) { array[i] = i; } } // Will always return the correct value? public int get(int index) { return array[index]; } } As far as I know the JMM guarantees that the value of final fields will be visible to other threads after construction. But I want to ensure that