immutability

Restrict mutable object inside immutable object Java

删除回忆录丶 提交于 2019-12-23 18:05:38
问题 I am learning about immutable Objects. I am trying this code public final class ImmutableObject { private final String name; private final NormalObject obj = new NormalObject(); public String getName() { return name; } public ImmutableObject(String name) { this.name = name; obj.setName(name); } public NormalObject getObj() { NormalObject tempObj = obj; return tempObj; } } public class NormalObject { private String name; public String getName() { return name; } public void setName(String name)

Immutable in java

℡╲_俬逩灬. 提交于 2019-12-23 17:15:55
问题 In Effective Java, Bloch recommends to make all the fields final in making an object immutable . Is it necessary to do so ? Won't just not giving accessor methods make it immutable. For example class A { private int x; A (int x) { this.x = x; } } The above class is immutable even if I don't declare x as final right ? Am I missing something ? 回答1: In addition to @Bozho's point, declaring a field as final means that it can be safely accessed without any synchronization. By contrast, if the

How to create an immutable dictionary in python?

时间秒杀一切 提交于 2019-12-23 16:07:10
问题 I want to subclass dict in python such that all the dictionaries of the sub-class are immutable. I don't understand how does __hash__ affects the immutability, since in my understanding it just signifies the equality or non-equality of objects ! So, can __hash__ be used to implement immutability ? How ? Update : Objective is that common response from an API is available as a dict, which has to be shared as a global variable. So, that needs to be intact no matter what ? 回答1: I found an

Making a heap copy of a struct in D

我们两清 提交于 2019-12-23 15:37:11
问题 How can I create a garbage-collected copy of a struct that's on the stack? Coming from a C++ background, my first guess would be a copy constructor like the one below, but it doesn't seem very idiomatic for D, and I haven't seen one in any of the D projects I've taken a look at. struct Foo { immutable int bar; this(int b) { bar = b; } // A C++-style copy constructor works but doesn't seem idiomatic. this(ref const Foo f) { bar = f.bar; } } void main() { // We initialize a Foo on the stack

Mutable MultiMap to immutable Map

二次信任 提交于 2019-12-23 13:26:14
问题 I create a MultiMap val ms = new collection.mutable.HashMap[String, collection.mutable.Set[String]]() with collection.mutable.MultiMap[String, String] which, after it has been populated with entries, must be passed to a function that expects a Map[String, Set[String]] . Passing ms directly doesn't work, and trying to convert it into a immutable map via toMap ms.toMap[String, Set[String]] yields Cannot prove that (String, scala.collection.mutable.Set[String]) <:< (String, Set[String]). Can

Mutability of the Iterator Element in a For-In loop with an Array in Swift

岁酱吖の 提交于 2019-12-23 13:24:28
问题 I have some code in Swift 3.0 like so for trying to update the property in a array of elements... for point in listOfPoints { var pointInFrame : Float = Float(point.position.x * sensorIncomingViewPortSize.width) + Float(point.position.y) point.status = getUpdateStatus( pointInFrame ) } However I get a compile error of: 'Cannot assign to property: 'point' is a 'let' constant' [for line 3] Is there anyway to make the iterator (point) mutable in Swift, like how you can use 'inout' for a function

Mutability of the Iterator Element in a For-In loop with an Array in Swift

只愿长相守 提交于 2019-12-23 13:24:21
问题 I have some code in Swift 3.0 like so for trying to update the property in a array of elements... for point in listOfPoints { var pointInFrame : Float = Float(point.position.x * sensorIncomingViewPortSize.width) + Float(point.position.y) point.status = getUpdateStatus( pointInFrame ) } However I get a compile error of: 'Cannot assign to property: 'point' is a 'let' constant' [for line 3] Is there anyway to make the iterator (point) mutable in Swift, like how you can use 'inout' for a function

Is it ok to compare immutable objects in Java using == instead of equals

柔情痞子 提交于 2019-12-23 09:19:34
问题 Consider two references of type Integer that call the static factory method valueOf as shown below:- Integer a = Integer.valueOf("10"); Integer b = Integer.valueOf("10"); Considering that Integer is immutable, is it ok to compare a and b using == instead of using equals method. I am guessing that the valueOf method makes sure that only one instance of Integer with the value 10 is created and a reference to this instance is returned for every Integer created with a value 10. In general, is it

What are immutable objects?

 ̄綄美尐妖づ 提交于 2019-12-23 09:04:21
问题 What is the relationship with thread-safety and immutable objects? Does it makes easier to share a single resource among multiple threads? If immutable objects are stateless, can they be pooled in a container like a J2EE container? thanks 回答1: Threadsafe objects are objects which allow to be accessed concurrently by multiple threads. Their implementation guarantees (for example by lockings / synchronzized methods / ...) that they will not get into a invalid state. In addition, there should be

Immutable class vs Immutable struct

狂风中的少年 提交于 2019-12-23 07:28:28
问题 I have a class that started life being mutable, but I've since made it immutable. Should I change it to be a struct ? What sort of considerations go into choosing one over the other? My particular case is a Point type class (it represents a coordinate in a custom coordinate system) that is made up of 4 int fields, plus a few properties to access the same data in different ways. I note that String is a class and is immutable, so there must be some use-case for this. 回答1: Generally, no, you