Questions tagged [mutable]

A mutable can be modified after it is created.

A mutable can be modified after it is created. It is a keyword in functional programming opposite to immutable.

Related tags:

1184 questions
43
votes
6 answers

Are strings in Ruby mutable?

Consider the following code: $ irb > s = "asd" > s.object_id # prints 2171223360 > s[0] = ?z # s is now "zsd" > s.object_id # prints 2171223360 (same as before) > s += "hello" # s is now "zsdhello" > s.object_id # prints 2171224560 (now…
SundayMonday
  • 19,147
  • 29
  • 100
  • 154
42
votes
1 answer

How to make any view to draw to canvas?

I have a short question: Suppose I have a (mutable) bitmap that I need to modify (add images, texts, etc...) . Instead of messing around with many special classes for drawing to the canvas (paint, canvas, matrices and so on), I was thinking why not…
android developer
  • 114,585
  • 152
  • 739
  • 1,270
41
votes
6 answers

NSArray @property backed by a NSMutableArray

I've defined a class where I'd like a public property to appear as though it is backed by an NSArray. That is simple enough, but in my case the actual backing ivar is an NSMutableArray: @interface Foo { NSMutableArray* array; } @property…
Matty P
  • 881
  • 1
  • 7
  • 11
41
votes
2 answers

Why are integers immutable in Python?

I understand the differences between mutable and immutable objects in Python. I have read many posts discussing the differences. However, I have not read anything regarding WHY integers are immutable objects. Does there exist a reason for this? Or…
Izzo
  • 4,461
  • 13
  • 45
  • 82
38
votes
5 answers

Why is there no mutable TreeMap in Scala?

Is it lack of time, some technical problem or is there a reason why it should not exist?
soc
  • 27,983
  • 20
  • 111
  • 215
37
votes
5 answers

Why does using `arg=None` fix Python's mutable default argument issue?

I'm at the point in learning Python where I'm dealing with the Mutable Default Argument problem. # BAD: if `a_list` is not passed in, the default will wrongly retain its contents between successive function calls def bad_append(new_item,…
75th Trombone
  • 1,364
  • 15
  • 28
36
votes
3 answers

Are strings mutable in Ruby?

Are Strings mutable in Ruby? According to the documentation doing str = "hello" str = str + " world" creates a new string object with the value "hello world" but when we do str = "hello" str << " world" It does not mention that it creates a new…
Aly
  • 15,865
  • 47
  • 119
  • 191
33
votes
4 answers

Correct Style for Python functions that mutate the argument

I would like to write a Python function that mutates one of the arguments (which is a list, ie, mutable). Something like this: def change(array): array.append(4) change(array) I'm more familiar with passing by value than Python's setup…
Neil Du Toit
  • 441
  • 1
  • 4
  • 4
32
votes
5 answers

Using volatile keyword with mutable object

In Java, I understand that volatile keyword provides visibility to variables. The question is, if a variable is a reference to a mutable object, does volatile also provide visibility to the members inside that object? In the example below, does it…
Hongbo
  • 1,107
  • 2
  • 11
  • 18
31
votes
4 answers

What is a "mostly complete" (im)mutability approach for C#?

Since immutability is not fully baked into C# to the degree it is for F#, or fully into the framework (BCL) despite some support in the CLR, what's a fairly complete solution for (im)mutability for C#? My order of preference is a solution consisting…
Kit
  • 20,354
  • 4
  • 60
  • 103
31
votes
3 answers

How are mutable arrays implemented in Haskell?

I've read many research papers on this topic, and they usually argue that arrays are implemented using Monads. But none of these papers gave a clear definition of how the "type" Array itself should be defined, they only gave definitions for the…
is7s
  • 3,500
  • 1
  • 20
  • 41
31
votes
4 answers

are user defined classes mutable

Say I want to create a class for car, tractor and boat. All these classes have an instance of engine and I want to keep track of all the engines in a single list. If I understand correctly if the motor object is mutable i can store it as an…
jonathan topf
  • 7,897
  • 17
  • 55
  • 85
28
votes
4 answers

Is making in-place operations return the object a bad idea?

I'm talking mostly about Python here, but I suppose this probably holds for most languages. If I have a mutable object, is it a bad idea to make an in-place operation also return the object? It seems like most examples just modify the object and…
asmeurer
  • 86,894
  • 26
  • 169
  • 240
28
votes
5 answers

Clojure mutable storage types

I'm attempting to learn Clojure from the API and documentation available on the site. I'm a bit unclear about mutable storage in Clojure and I want to make sure my understanding is correct. Please let me know if there are any ideas that I've gotten…
Kai
  • 9,444
  • 6
  • 46
  • 61
27
votes
1 answer

How can I do a mutable borrow in a for loop?

I tried: fn main() { let mut vec = [1, 2, 3]; for mut x in &vec { *x = 3; } for mut &x in &vec { x = 3; } for mut *x in &vec { x = 3; } for mut x in mut &vec { *x = 3; } for mut x in &(mut vec) { *x = 3; } } None of…
ZisIsNotZis
  • 1,570
  • 1
  • 13
  • 30
1 2
3
78 79