Questions tagged [immutability]

Immutability is the inability to modify data after it has been created. Modifications are instead made by copying the data. A property of immutable data is that it is *referentially transparent*.

Overview

Immutability is the inability to modify a variable after it is has been created.

It is a pattern found in many branches of programming; immutable objects are used widely within object oriented languages (such as Python's str type, Java's String and Integer type, .NET's System.String, etc.), functional programming (esp. Haskell and other pure languages), and other paradigms.

See also

3670 questions
92
votes
14 answers

Immutable class?

How can one make a Java class immutable, what is the need of immutability and is there any advantage to using this?
JavaUser
  • 25,542
  • 46
  • 113
  • 139
92
votes
7 answers

Immutable/Mutable Collections in Swift

I was referring to Apple's Swift programming guide for understanding creation of Mutable/ immutable objects(Array, Dictionary, Sets, Data) in Swift language. But I could't understand how to create a immutable collections in Swift. I would like to…
Pranav Jaiswal
  • 3,752
  • 3
  • 32
  • 50
89
votes
12 answers

In Python, how can I make unassignable attributes (like ones marked with `final` in Java)?

Is there anything in Python that works like the final keyword in Java - i.e., to disallow assigning to a specific attribute of the instances of a class, after those instances have been created? I couldn't find anything like this in the…
Jason Coon
  • 17,601
  • 10
  • 42
  • 50
86
votes
14 answers

How to create immutable objects in Java?

How to create immutable objects in Java? Which objects should be called immutable? If I have class with all static members is it immutable?
Neel Salpe
  • 1,287
  • 3
  • 15
  • 26
85
votes
8 answers

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

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[]…
Cui Pengfei 崔鹏飞
  • 8,017
  • 6
  • 46
  • 87
84
votes
6 answers

Set System.Drawing.Color values

Hi how to set R G B values in System.Drawing.Color.G ? which is like System.Drawing.Color.G=255; is not allowed because its read only Property or indexer 'System.Drawing.Color.G' cannot be assigned toit is read only i just need to create a Color…
Sudantha
  • 15,684
  • 43
  • 105
  • 161
83
votes
4 answers

Converting mutable to immutable map

private[this]object MMMap extends HashMap[A, Set[B]] with MultiMap[A, B] How convert it to immutable?
Jeriho
  • 7,129
  • 9
  • 41
  • 57
83
votes
2 answers

How do I create a Vec from a range and shuffle it?

I have the following code: extern crate rand; use rand::{thread_rng, Rng}; fn main() { let mut vec: Vec = (0..10).collect(); let mut slice: &[u32] = vec.as_mut_slice(); thread_rng().shuffle(slice); } and get the following…
le_me
  • 3,089
  • 3
  • 26
  • 28
82
votes
9 answers

What's the use of System.String.Copy in .NET?

I'm afraid that this is a very silly question, but I must be missing something. Why might one want to use String.Copy(string)? The documentation says the method Creates a new instance of String with the same value as a specified String. Since…
Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
81
votes
5 answers

Are Elixir variables really immutable?

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…
Odhran Roche
  • 1,111
  • 1
  • 7
  • 14
80
votes
7 answers

Why shouldn't I use immutable POJOs instead of JavaBeans?

I have implemented a few Java applications now, only desktop applications so far. I prefer to use immutable objects for passing the data around in the application instead of using objects with mutators (setters and getters), also called…
Jonas
  • 121,568
  • 97
  • 310
  • 388
79
votes
8 answers

How can I create a new instance of ImmutableDictionary?

I would like to write something like this: var d = new ImmutableDictionary { { "a", 1 }, { "b", 2 } }; (using ImmutableDictionary from System.Collections.Immutable). It seems like a straightforward usage as I am declaring all the…
Lukáš Lánský
  • 4,641
  • 3
  • 32
  • 48
78
votes
7 answers

Is there a way to Object.freeze() a JavaScript Date?

According to MDN Object.freeze() documentation: The Object.freeze() method freezes an object: that is, prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their…
Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
77
votes
8 answers

Immutable objects that reference each other?

Today I was trying to wrap my head around immutable objects that reference each other. I came to the conclusion that you can't possibly do that without using lazy evaluation but in the process I wrote this (in my opinion) interesting code. public…
Stilgar
  • 22,354
  • 14
  • 64
  • 101
73
votes
4 answers

Why are strings immutable in many programming languages?

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…
snowfox
  • 1,978
  • 1
  • 21
  • 21