immutability

How do I make a custom immutable type using facebook/immutable-js?

这一生的挚爱 提交于 2020-01-25 00:29:08
问题 I am attempting to make a simple immutable Interval type. It has a start and an end property. Is there a straightforward way to utilize the immutable-js library to create your own custom immutable types? My current attempt at using a containment pattern with Immutable.Map and Immutable.List is not working so well. difference , for example, should return an immutable list of Intervals, but because it creates new instances of Interval, the List it returns will not pass equality. It feels like I

In Rust, what exactly are mutable and immutable borrows?

久未见 提交于 2020-01-24 13:53:06
问题 I'm stuck with the Rust concepts of borrowing and mutable : #[derive(Debug)] struct Rectangle { height: u32, width: u32, } fn mut_area(rect_mut: &mut Rectangle) -> u32 { rect_mut.width /= 2; rect_mut.height * rect_mut.width } fn mut_string(s: &mut String) -> &str { s.push_str("!"); let len = s.len(); &s[0..len / 2] } fn main() { let mut rect = Rectangle { height: 50, width: 40, }; println!("original rect: {:?}", rect); let a = mut_area(&mut rect); println!("area of rect: {}", a); println!(

In Rust, what exactly are mutable and immutable borrows?

烈酒焚心 提交于 2020-01-24 13:52:00
问题 I'm stuck with the Rust concepts of borrowing and mutable : #[derive(Debug)] struct Rectangle { height: u32, width: u32, } fn mut_area(rect_mut: &mut Rectangle) -> u32 { rect_mut.width /= 2; rect_mut.height * rect_mut.width } fn mut_string(s: &mut String) -> &str { s.push_str("!"); let len = s.len(); &s[0..len / 2] } fn main() { let mut rect = Rectangle { height: 50, width: 40, }; println!("original rect: {:?}", rect); let a = mut_area(&mut rect); println!("area of rect: {}", a); println!(

Why BigInteger in java is designed to be immutable?

旧街凉风 提交于 2020-01-23 05:37:05
问题 In java , BigInteger is immutable but I want to understand why because a lot of times it is used to do a lot of calculations which can produce a lot of objects. So , it feels kind of intuitive to not make it immutable. The situation which comes to my mind is something like of string operations and then the option of StringBuilder. Should there be non-immutable counterpart of BigInteger ? I think it might be beneficial in a lot of situations. Edit: I know the benefits of immutability and how

Value-based Classes confusion

南楼画角 提交于 2020-01-22 09:42:04
问题 I'm seeking some clarification to the definition of Value-based Classes. I can't imagine, how is the last bullet point (6) supposed to work together with the first one (1) they are final and immutable ( though may contain references to mutable objects ) (6) they are freely substitutable when equal, meaning that interchanging any two instances x and y that are equal according to equals() in any computation or method invocation should produce no visible change in behavior. Optional is such a

Immutable Struct in Golang

佐手、 提交于 2020-01-21 02:38:09
问题 Is it possible to define an immutable struct in Golang? Once initialized then only read operation on struct's field, no modification of field values. If so, how to do that. 回答1: It is possible to make a struct to be read-only outside of package by making it's members non-exported and providing readers. For example: package mypackage type myReadOnly struct { value int } func (s myReadOnly) Value() int { return s.value } func NewMyReadonly(value int) myReadOnly{ return myReadOnly{value: value}

Immutable string and pointer address

穿精又带淫゛_ 提交于 2020-01-20 06:26:05
问题 In Go spec is written: Strings are immutable: once created, it is impossible to change the contents of a string. I have following code: str := "hello" fmt.Printf("%p\n",&str) // 0x1040c128 fmt.Printf("%v\n",str) // hello ptr := &str *ptr = "world" fmt.Printf("%p\n",&str) // 0x1040c128 fmt.Printf("%v\n",str) // world I would have expected &str address was changed after *ptr = "world" . As it would happen with Java, where we reassign String references. What is 'immutability' here? 回答1: string

Is this a good practice of immutability?

时光怂恿深爱的人放手 提交于 2020-01-17 01:22:07
问题 Good morning, Suppose I have a class public class Class { int something; int[] otherThing; } and I want to make objects of type Class immutable. Suppose also that I have a very frequent operation which creates a new object of type Class , public Class SomeFunction() { int[] Temp = new int[] { ... }; return new Class(1, Temp); } To avoid creating new objects too often, and since Temp is no longer accessible out of the method, is it too bad to set on the constructor this.otherThing = Temp;

Why can tuples contain mutable items?

跟風遠走 提交于 2020-01-16 08:35:12
问题 If a tuple is immutable then why can it contain mutable items? It is seemingly a contradiction that when a mutable item such as a list does get modified, the tuple it belongs to maintains being immutable. 回答1: That's an excellent question. The key insight is that tuples have no way of knowing whether the objects inside them are mutable. The only thing that makes an object mutable is to have a method that alters its data. In general, there is no way to detect this. Another insight is that

Safe publication of array/collection/map contents written once

我的梦境 提交于 2020-01-16 08:08:05
问题 Main question: What is the best way to perform safe publication of the contents of an array, collection, or map in Java? Here is what I've tried and some side questions I have: Side question #1 On thread 1, I'm writing to an HashMap: Map<K, V> map = new HashMap<>(); map.put(key, value); My current understanding is that: Collections.unmodifiableMap() does not constitute safe publication of the references stored in the map. By examining these particular implementations of Map.of(), the result