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
10
votes
1 answer

Cannot borrow data mutably in a `&` reference in array

I want to change a value of a struct in an array in another struct: struct Foo<'a> { bar: &'a [&'a mut Bar] } struct Bar { baz: u16 } impl<'a> Foo<'a> { fn add(&mut self, x: u16) { self.bar[0].add(x); } } impl Bar { fn…
Charlie
  • 978
  • 1
  • 7
  • 27
10
votes
3 answers

How can I clear the contents of an NSMutableAttributedString?

I have an ivar which is alloc-inited in the init of an object: attString = [[NSMutableAttributedString alloc] init]; On a loop, I want to clear the contents of attString and re-use it. How do I do this? Thanks!
jowie
  • 8,028
  • 8
  • 55
  • 94
10
votes
1 answer

In F#, is it possible to pass a reference to a mutable, defaulted value as a parameter?

For the Froto project (Google Protobuf in F#), I am trying to update the deserialization code from using 'a ref objects to passing values byref<'a>, for performance. However, the code below fails on the hydrator &element field line: type Field =…
James Hugard
  • 3,232
  • 1
  • 25
  • 36
10
votes
1 answer

Best approach to mutate(add/remove bindings) a Guice injector while maintaining state

I am hoping to redefine or update some bindings within a Module which is in turn used by an Injector. I realise that Modules are immutable and once a binding is sucked and injected its definition cannot change for all practical purposes. I would…
mP.
  • 18,002
  • 10
  • 71
  • 105
10
votes
1 answer

How to implement actions in ST-monad with my own underlying representation (similarly to STRef or STArray) using simple techniques?

I want to manipulate structs of a certain type from FFI through an interface like that provided with STArray or STRef in an ST monad. I'll have my own specific methods with understandable names for the kind of manipulations that are useful for this…
imz -- Ivan Zakharyaschev
  • 4,921
  • 6
  • 53
  • 104
10
votes
1 answer

Understanding immutable composite types with fields of mutable types in Julia

Initial note: I'm working in Julia, but this question probably applies to many languages. Setup: I have a composite type as follows: type MyType x::Vector{String} end I write some methods to act on MyType. For example, I write a method that…
Colin T Bowers
  • 18,106
  • 8
  • 61
  • 89
10
votes
4 answers

Constant correctness and

What is the correct way to deal with (otherwise) constant functions that include a random generator call of C++11's random-class? Should you prefer giving up the constant flag of the function or would it be better to declare generator and…
user3058865
  • 460
  • 1
  • 4
  • 10
10
votes
1 answer

Why does Fsharp Interactive allow mutable variables to be captured by closures?

Using an example from Chris Smith's Programming F# 3.0: let invalidUseOfMutable() = let mutable x = 0 let incrementX() = x <- x + 1 incrementX() x;; This fails as expected: error FS0407: The mutable variable 'x' is used in an…
TrueWill
  • 25,132
  • 10
  • 101
  • 150
10
votes
4 answers

Are String Arrays mutable?

I wonder if String arrays in Java are mutable ? I know that Strings are immutable, but how about string Arrays ? If I have a string array, and change the content, will a new string object be created ? Or will the actual value just be changed…
mrjasmin
  • 1,230
  • 6
  • 21
  • 37
10
votes
1 answer

Preferred way of returning an immutable object

If I have a method that looks something like this: - (NSDictionary *)removeDataInDictionary:(NSDictionary *)dictionary { NSMutableDictionary *mutableDictionary = [dictionary mutableCopy]; [mutableDictionary removeObjectForKey:@"key"]; …
Peter Warbo
  • 11,136
  • 14
  • 98
  • 193
10
votes
4 answers

const_cast VS mutable ? any difference?

From my understanding , mutable cancels the constness of a variable Class A { void foo() const { m_a = 5; } mutable int m_a; }; But also const_cast : void print (char * str) { cout << str << endl; } int main () { const char * c = "this is…
JAN
  • 21,236
  • 66
  • 181
  • 318
9
votes
4 answers

AtomicReference to a mutable object and visibility

Say I have an AtomicReferenceto a list of objects: AtomicReference> batch = new AtomicReference>(new ArrayList()); Thread A adds elements to this list: batch.get().add(o); Later, thread B takes the list and, for…
Jan Van den bosch
  • 3,542
  • 3
  • 26
  • 38
9
votes
2 answers

Mutable, (possibly parallel) Haskell code and performance tuning

I have now implemented another SHA3 candidate, namely Grøstl. This is still work in progress (very much so), but at the moment a 224-bit version pass all KATs. So now I'm wondering about performance (again :->). The difference this time, is that I…
hakoja
  • 896
  • 7
  • 16
9
votes
4 answers

When does -copy return a mutable object?

I read in Cocoa and Objective C: Up and Running that -copy will always return an immutable object and -mutableCopy will always return a mutable object: It’s important to know that calling -copy on a mutable object returns an immutable version. If…
rubergly
  • 878
  • 8
  • 20
9
votes
2 answers

How borrow as mutable vs immutable in Rust?

I've read these docs: https://doc.rust-lang.org/rust-by-example/scope/borrow/mut.html I've also read this question: (Cannot borrow immutable borrowed content as mutable) The docs helped me understand how to declare borrowing as mutable (I…
Seph Reed
  • 8,797
  • 11
  • 60
  • 125