immutability

In C#, why can't I modify the member of a value type instance in a foreach loop?

こ雲淡風輕ζ 提交于 2019-11-27 04:28:26
问题 I know that value types should be immutable, but that's just a suggestion, not a rule, right? So why can't I do something like this: struct MyStruct { public string Name { get; set; } } public class Program { static void Main(string[] args) { MyStruct[] array = new MyStruct[] { new MyStruct { Name = "1" }, new MyStruct { Name = "2" } }; foreach (var item in array) { item.Name = "3"; } //for (int i = 0; i < array.Length; i++) //{ // array[i].Name = "3"; //} Console.ReadLine(); } } The foreach

Why are Java wrapper classes immutable?

拜拜、爱过 提交于 2019-11-27 04:20:58
问题 I know the usual reasons that apply to general immutable classes, viz can not change as a side effect easy to reason about their state inherently thread safe no need to provide clone/copy constructor/factory copy method instance caching no need for defensive copies. However, wrapper classes represent primitive types, and primitive types are mutable. So why aren't wrapper classes mutable? 回答1: However, wrapper classes represent primitive types, and primitive types (except String) are mutable.

Are value types immutable by definition?

拟墨画扇 提交于 2019-11-27 04:12:35
问题 I frequently read that struct s should be immutable - aren't they by definition? Do you consider int to be immutable? int i = 0; i = i + 123; Seems okay - we get a new int and assign it back to i . What about this? i++; Okay, we can think of it as a shortcut. i = i + 1; What about the struct Point ? Point p = new Point(1, 2); p.Offset(3, 4); Does this really mutate the point (1, 2) ? Shouldn't we think of it as a shortcut for the following with Point.Offset() returning a new point? p = p

Immutable Type: public final fields vs. getter

ε祈祈猫儿з 提交于 2019-11-27 03:53:00
I need a small Container-Class for storing some Strings which should be immutable. As String itself is an immutable type, I thought of something like that: public final class Immu { public final String foo; public final String bar; public Immu(final String foo, final String bar) { this.foo = foo; this.bar = bar; } } Many people seem to object using public fields at all and use Getters instead. IMHO this would be just boilerplate in this case, because String itself is immutable. Other thoughts I may be missing on this one? I would do what you believe is simplest and clearest. If you have a data

Are Elixir variables really immutable?

会有一股神秘感。 提交于 2019-11-27 03:47:14
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. Immutability means that data structures don't change. For example the function HashSet.new returns an empty set and as long as you hold on to

Deserializing ImmutableList using Gson

笑着哭i 提交于 2019-11-27 03:20:32
问题 I'm using quite a few immutable collections and I'm curious how to deserialize them using Gson. As nobody answered and I've found the solution myself, I'm simplifying the question and presenting my own answer. I had two problems: How to write a single Deserializer working for all ImmutableList<XXX> ? How to register it for all ImmutableList<XXX> ? 回答1: Update: There's https://github.com/acebaggins/gson-serializers which covers many guava collections: ImmutableList ImmutableSet

Swift: What's the best way to pair up elements of an Array

大憨熊 提交于 2019-11-27 02:53:29
问题 I came across a problem that required iterating over an array in pairs. What's the best way to do this? Or, as an alternative, what's the best way of transforming an Array into an Array of pairs (which could then be iterated normally)? Here's the best I got. It requires output to be a var , and it's not really pretty. Is there a better way? let input = [1, 2, 3, 4, 5, 6] var output = [(Int, Int)]() for i in stride(from: 0, to: input.count - 1, by: 2) { output.append((input[i], input[i+1])) }

What's the best name for a non-mutating “add” method on an immutable collection?

天大地大妈咪最大 提交于 2019-11-27 02:38:43
Sorry for the waffly title - if I could come up with a concise title, I wouldn't have to ask the question. Suppose I have an immutable list type. It has an operation Foo(x) which returns a new immutable list with the specified argument as an extra element at the end. So to build up a list of strings with values "Hello", "immutable", "world" you could write: var empty = new ImmutableList<string>(); var list1 = empty.Foo("Hello"); var list2 = list1.Foo("immutable"); var list3 = list2.Foo("word"); (This is C# code, and I'm most interested in a C# suggestion if you feel the language is important.

Are immutable arrays possible in .NET?

时光毁灭记忆、已成空白 提交于 2019-11-27 02:38:01
问题 Is it possible to somehow mark a System.Array as immutable. When put behind a public-get/private-set they can't be added to, since it requires re-allocation and re-assignment, but a consumer can still set any subscript they wish: public class Immy { public string[] { get; private set; } } I thought the readonly keyword might do the trick, but no such luck. 回答1: ReadOnlyCollection<T> is probably what you are looking for. It doesn't have an Add() method. 回答2: The Framework Design Guidelines

What does immutable mean?

*爱你&永不变心* 提交于 2019-11-27 02:29:52
If a string is immutable, does that mean that.... (let's assume JavaScript) var str = 'foo'; alert(str.substr(1)); // oo alert(str); // foo Does it mean, when calling methods on a string, it will return the modified string, but it won't change the initial string? If the string was mutable, does that mean the 2nd alert() would return oo as well? kemiller2002 It means that once you instantiate the object, you can't change its properties. In your first alert you aren't changing foo. You're creating a new string. This is why in your second alert it will show "foo" instead of oo. Does it mean, when