immutability

Allen Holub wrote “You should never use get/set functions”, is he correct? [duplicate]

南笙酒味 提交于 2019-12-17 05:40:18
问题 This question already has answers here : Why use getters and setters/accessors? (38 answers) Closed 4 years ago . Allen Holub wrote the following, You can't have a program without some coupling. Nonetheless, you can minimize coupling considerably by slavishly following OO (object-oriented) precepts (the most important is that the implementation of an object should be completely hidden from the objects that use it). For example, an object's instance variables (member fields that aren't

Why are strings immutable in many programming languages? [duplicate]

冷暖自知 提交于 2019-12-17 04:21:52
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why can't strings be mutable in Java and .NET? Why .NET String is immutable? Several languages have chosen for this, such as C#, Java, and Python. If it is intended to save memory or gain efficiency for operations like compare , what effect does it have on concatenation and other modifying operations? 回答1: Immutable types are a good thing generally: They work better for concurrency (you don't need to lock

Examples of immutable classes

自古美人都是妖i 提交于 2019-12-17 03:48:16
问题 I already know the definition of immutable classes but I need a few examples. 回答1: Some famous immutable classes in the Standard API: java.lang.String (already mentioned) The wrapper classes for the primitive types: java.lang.Integer, java.lang.Byte, java.lang.Character, java.lang.Short, java.lang.Boolean, java.lang.Long, java.lang.Double, java.lang.Float java.lang.StackTraceElement (used in building exception stacktraces) Most enum classes are immutable, but this in fact depends on the

Immutable vs Unmodifiable collection

廉价感情. 提交于 2019-12-17 03:21:48
问题 From the Collections Framework Overview: Collections that do not support modification operations (such as add , remove and clear ) are referred to as unmodifiable . Collections that are not unmodifiable are modifiable . Collections that additionally guarantee that no change in the Collection object will be visible are referred to as immutable . Collections that are not immutable are mutable . I cannot understand the distinction. What is the difference between unmodifiable and immutable here?

Immutable class?

走远了吗. 提交于 2019-12-17 02:08:09
问题 How can one make a Java class immutable, what is the need of immutability and is there any advantage to using this? 回答1: What is an immutable object? An immutable object is one that will not change state after it is instantiated. How to make an object immutable? In general, an immutable object can be made by defining a class which does not have any of its members exposed, and does not have any setters. The following class will create an immutable object: class ImmutableInt { private final int

Const in JavaScript: when to use it and is it necessary?

天涯浪子 提交于 2019-12-16 22:18:10
问题 I've recently come across the const keyword in JavaScript. From what I can tell, it is used to create immutable variables, and I've tested to ensure that it cannot be redefined (in Node.js): const x = 'const'; const x = 'not-const'; // Will give an error: 'constant 'x' has already been defined' I realise that it is not yet standardized across all browsers - but I'm only interested in the context of Node.js V8, and I've noticed that certain developers / projects seem to favor it heavily when

Const in JavaScript: when to use it and is it necessary?

老子叫甜甜 提交于 2019-12-16 22:17:59
问题 I've recently come across the const keyword in JavaScript. From what I can tell, it is used to create immutable variables, and I've tested to ensure that it cannot be redefined (in Node.js): const x = 'const'; const x = 'not-const'; // Will give an error: 'constant 'x' has already been defined' I realise that it is not yet standardized across all browsers - but I'm only interested in the context of Node.js V8, and I've noticed that certain developers / projects seem to favor it heavily when

Swift error: Cannot assign to immutable value

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-14 03:53:15
问题 I got the error above for this code snippet: func store(name: String, inout array: [AnyObject]) { for object in array { if object is [AnyObject] { store(name, &object) return } } array.append(name) } Any ideas? 回答1: the item object extracted with for is immutable. You should iterate indices of the array instead. And, the item is AnyObject you cannot pass it to inout array: [AnyObject] parameter without casting. In this case, you should cast it to mutable [AnyObject] and then reassign it: func

Creating Immutable Classes with Multiple Constructors

老子叫甜甜 提交于 2019-12-14 03:49:10
问题 I'm reading this page on creating immutable classes in Java, and decided to modify a class I was writing to the specifications outlined on the page. final String personfirstname; final String personlastname; final int personAge; public Person(String firstname, String lastname) { this.personfirstname = firstname; this.personlastname = lastname; } public Person(String firstname, String lastname, int personAge){ this(firstname,lastname); this.personAge = personAge; } My issue is, eclipse says

Scala - why does :: not change a List? [duplicate]

戏子无情 提交于 2019-12-14 03:32:50
问题 This question already has answers here : Immutable Lists in Scala (5 answers) Closed 4 years ago . I have just written this code for some fun and I have a question why it isn't working out? val list = List[Int]() while (list.length < 20) { Random.nextInt(100) :: list } println(list) } Seems like nothing is written to the list, but why is that so? Do I have to make it mutable? And why is here the :: operator not working properly? 回答1: Because x :: xs returns a new list, where the first element