immutability

Why final keyword is necessary for immutable class?

▼魔方 西西 提交于 2019-11-28 04:34:05
Could you please clarify that why final keyword is required before class when we are making it an immutable one. I mean, if we declare all of it's attributes as private and final, then also it is an immutable class, isn't it? Sorry if the question seems easy, but i am truly confused about it. Help me out. Editted: I know that a class declared final can't be subclassed.. But if each attribute is private and final then what difference does that make? Jon Skeet As stacker says, final makes sure the class isn't subclassed. That's important so that any code which is relying on its immutability can

Swift make method parameter mutable?

无人久伴 提交于 2019-11-28 04:26:38
How can I deal with this error without creating additional variable? func reduceToZero(x:Int) -> Int { while (x != 0) { x = x-1 // ERROR: cannot assign to 'let' value 'x' } return x } I don't want to create additional variable just to store the value of x. Is it even possible to do what I want? As stated in other answers, as of Swift 3 placing var before a variable has been deprecated. Though not stated in other answers is the ability to declare an inout parameter. Think: passing in a pointer. func reduceToZero(_ x: inout Int) { while (x != 0) { x = x-1 } } var a = 3 reduceToZero(&a) print(a)

NSString immutable allows to change its values?

落花浮王杯 提交于 2019-11-28 04:12:54
问题 The code below compiles and runs, BUT according to all iPhone development books and Apple documentation it shouldnt! Can someone please explain to me how come immutable NSString allows to change its values after it has been set? I thought I had to use NSMuttableString to change context of the same string variable? I am using SDK 3.1. NSString *test =[[NSString alloc] initWithString:@"TEST"]; [test release]; test = @"TEST2"; 回答1: Perhaps the following example will add upon the replies from

Immutable collections?

风格不统一 提交于 2019-11-28 03:43:12
I am making most of my basic types in my app, immutable. But should the collections be immutable too? To me, this seems like a huge overhead unless I am missing something. I am talking about collections to hold Point3 values, etc which can be added as it goes at different times. So if there are 1M values in a collection, and you needed to delete 1 of them, you would have to create the same collection all over again, right? Joel Coehoorn Eric Lippert has a series on Immutability in C#, and if you read it all the way through he implements a couple different immutable collections: Immutability in

Implement an immutable deque as a balanced binary tree?

非 Y 不嫁゛ 提交于 2019-11-28 03:41:43
I've been thinking for a while about how to go about implementing a deque (that is, a double-ended queue) as an immutable data structure. There seem to be different ways of doing this. AFAIK, immutable data structures are generally hierarchical , so that major parts of it can be reused after modifying operations such as the insertion or removal of an item. Eric Lippert has two articles on his blog about this topic, along with sample implementations in C#. Both of his implementations strike me as more elaborate than is actually necessary. Couldn't deques simply be implemented as binary trees,

What is difference between mutable and immutable String in java

泪湿孤枕 提交于 2019-11-28 03:39:56
As per my knowledge, a mutable string can be changed, and an immutable string cannot be changed. Here I want to change the value of String like this, String str="Good"; str=str+" Morning"; and other way is, StringBuffer str= new StringBuffer("Good"); str.append(" Morning"); In both the cases I am trying to alter the value of str . Can anyone tell me, what is difference in both case and give me clear picture of mutable and immutable objects. TheLostMind Case 1: String str = "Good"; str = str + " Morning"; In the above code you create 3 String Objects. "Good" it goes into the String Pool . "

Under which circumstances do equal strings share the same reference?

风格不统一 提交于 2019-11-28 03:29:21
问题 I have searched the web and stack overflow questions but been unable to find an answer to this question. The observation that I've made is that in Python 2.7.3, if you assign two variables the same single character string, e.g. >>> a = 'a' >>> b = 'a' >>> c = ' ' >>> d = ' ' Then the variables will share the same reference: >>> a is b True >>> c is d True This is also true for some longer strings: >>> a = 'abc' >>> b = 'abc' >>> a is b True >>> ' ' is ' ' True >>> ' ' * 1 is ' ' * 1 True

What is immutability and why should I worry about it?

泪湿孤枕 提交于 2019-11-28 02:59:09
I've read a couple of articles on immutability but still don't follow the concept very well. I made a thread on here recently which mentioned immutability, but as this is a topic in itself, I am making a dedicated thread now. I mentioned in the past thread that I thought immutability is the process of making an object read only and giving it low visibility. Another member said it didn't really have anything to do with that. This page (part of a series ) uses an example of an immutable class/struct and it uses readonly and other concepts to lock it down. What exactly is the definition of state

When does `modify` copy the vector?

给你一囗甜甜゛ 提交于 2019-11-28 02:50:48
问题 From https://hackage.haskell.org/package/vector-0.12.0.1/docs/Data-Vector.html#v:modify Apply a destructive operation to a vector. The operation will be performed in place if it is safe to do so and will modify a copy of the vector otherwise. This sounds like it can have drastically different performance characteristics depending on whether it is deemed "safe" to modify the vector in place. This motivates the questions... When will the modify be performed in place, and when will the vector be

Why it's impossible to override `var` with `def` in Scala?

北战南征 提交于 2019-11-28 02:01:47
While I understand why a var cannot override a val in subclass and vice versa, I am unable to understand why does Scala not allow a def in subclass to override a var in superclass class Car { var age = 32 } class SedanCar extends Car { override def age = 54 } As var is mutable why not allow a def to override it? Can anyone please help me in understanding this? dk14 That's related to the Liskov Substitution Principle : you can't assign weaker access privileges in subclass (even for Java) . Making var a def makes the setter def x_= (y: T ): Unit private (as @Staix said). So in case when Seadan