immutability

Python string with space and without space at the end and immutability

拜拜、爱过 提交于 2019-11-26 19:05:30
I learnt that in some immutable classes, __new__ may return an existing instance - this is what the int , str and tuple types sometimes do for small values. But why do the following two snippets differ in the behavior? With a space at the end: >>> a = 'string ' >>> b = 'string ' >>> a is b False Without a space: >>> c = 'string' >>> d = 'string' >>> c is d True Why does the space bring the difference? This is a quirk of how the CPython implementation chooses to cache string literals. String literals with the same contents may refer to the same string object, but they don't have to. 'string'

Immutable object pattern in C# - what do you think? [closed]

这一生的挚爱 提交于 2019-11-26 18:48:39
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . I have over the course of a few projects developed a pattern for creating immutable (readonly) objects and immutable object graphs. Immutable objects carry the benefit of being 100% thread safe and can therefore be reused across threads. In my work I very often use this pattern in

Building big, immutable objects without using constructors having long parameter lists

拜拜、爱过 提交于 2019-11-26 18:46:30
问题 I have some big (more than 3 fields) objects that can and should be immutable. Every time I run into that case I tend to create constructor abominations with long parameter lists. It doesn't feel right, it is hard to use, and readability suffers. It is even worse if the fields are some sort of collection type like lists. A simple addSibling(S s) would ease the object creation so much but renders the object mutable. What do you guys use in such cases? I'm on Scala and Java, but I think the

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

点点圈 提交于 2019-11-26 18:38:38
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? Immutable types are a good thing generally: They work better for concurrency (you don't need to lock something that can't change!) They reduce errors: mutable objects are vulnerable to being changed when you don't expect it which can introduce all kinds of strange bugs (

Scala - initialization order of vals

心已入冬 提交于 2019-11-26 18:29:04
问题 I have this piece of code that loads Properties from a file: class Config { val properties: Properties = { val p = new Properties() p.load(Thread.currentThread().getContextClassLoader.getResourceAsStream("props")) p } val forumId = properties.get("forum_id") } This seems to be working fine. I have tried moving the initialization of properties into another val, loadedProperties , like this: class Config { val properties: Properties = loadedProps val forumId = properties.get("forum_id") private

a mutable type inside an immutable container

牧云@^-^@ 提交于 2019-11-26 17:55:55
I'm a bit confused about modifying tuple members. The following doesn't work: >>> thing = (['a'],) >>> thing[0] = ['b'] TypeError: 'tuple' object does not support item assignment >>> thing (['a'],) But this does work: >>> thing[0][0] = 'b' >>> thing (['b'],) Also works: >>> thing[0].append('c') >>> thing (['b', 'c'],) Doesn't work, and works (huh?!): >>> thing[0] += 'd' TypeError: 'tuple' object does not support item assignment >>> thing (['b', 'c', 'd'],) Seemingly equivalent to previous, but works: >>> e = thing[0] >>> e += 'e' >>> thing (['b', 'c', 'd', 'e'],) So what exactly are the rules

.clone() or Arrays.copyOf()?

爷,独闯天下 提交于 2019-11-26 17:41:44
In an effort to reduce mutability, should we rather use public void setValues(String[] newVals) { this.vals = ( newVals == null ? null : newVals.clone() ); } or public void setValues(String[] newVals) { this.vals = ( newVals == null ? null : Arrays.copyOf(newVals, newVals.length) ); } assylias Update using jmh Using jmh , I get similar results, except that clone seems to be marginally better. Original post I ran a quick test for performance: clone , System.arrayCopy and Arrays.copyOf have very similar performance (jdk 1.7.06, server vm). For details (in ms), after JIT: clone: 68 arrayCopy: 68

What's the advantage of a String being Immutable?

强颜欢笑 提交于 2019-11-26 17:36:25
Once I studied about the advantage of a string being immutable because of something to improve performace in memory. Can anybody explain this to me? I can't find it on the Internet. Immutability (for strings or other types) can have numerous advantages: It makes it easier to reason about the code, since you can make assumptions about variables and arguments that you can't otherwise make. It simplifies multithreaded programming since reading from a type that cannot change is always safe to do concurrently. It allows for a reduction of memory usage by allowing identical values to be combined

Are Elixir variables really immutable?

只愿长相守 提交于 2019-11-26 17:32:00
问题 In Dave Thomas's book Programming Elixir he states "Elixir enforces immutable data" and goes on to say: In Elixir, once a variable references a list such as [1,2,3], you know it will always reference those same values (until you rebind the variable). This sounds like "it won't ever change unless you change it" so I'm confused as to what the difference between mutability and rebinding is. An example highlighting the differences would be really helpful. 回答1: Immutability means that data

Why is it possible to implement Read on an immutable reference to File?

让人想犯罪 __ 提交于 2019-11-26 17:24:25
问题 If you check out the docs for Read, most of the methods accept a &mut self . This makes sense, as reading from something usually updates an internal offset so the next read returns different data. However, this compiles: use std::io::Read; use std::fs::File; fn main() { let file = File::open("/etc/hosts").unwrap(); let vec = &mut Vec::new(); (&file).read_to_end(vec).unwrap(); println!("{:?}", vec); } The file isn't mutable, but the data is certainly being read in. This seems incorrect to me.