Questions tagged [mutable]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
7 votes
1 answer
286 views

Can functional programming languages have deadlock conditions?

I am reading through "Clean Architecture: A Craftsman's Guide to Software Structure and Design" and it says that: All race conditions, deadlock conditions, and concurrent update problems are ...
0 votes
1 answer
137 views

Would an mutable to immutable transpiler be possible?

I'm very new to writing immutable code. Would there be any fundamental issues trying to transpile arbitrary mutable code person.age = 20 into immutable code person = { ...person, age: 21 }? I am aware ...
6 votes
3 answers
639 views

How could a computer program do anything if everything is immutable?

I feel this is a bad question because I probably do not understand what I am talking about. In my effort to learn about functional programming, I became stumped on understanding the idea of immutable ...
  • 87
1 vote
4 answers
432 views

How to avoid global mutable variables within a class?

I know the fact that global mutable variables are bad as they can be accidentally modified and make testing difficult. However, there are situations when a class needs to cache some information, for e....
1 vote
2 answers
202 views

Is there a natural architecture design where I should use mutable member variable(c++)?

I have come across mutable variables kind often, yet it seems to me it's always been use to solve a problem of design(typically coping with legacy code) rather than to answer a natural, intrinsic ...
7 votes
4 answers
4k views

Is the meaning of `const` still thread-safe in C++11?

I recently came across Herb Sutter's video from about how the meaning of const and mutable has changed in C++11 to mean bitwise const (and thread-safe, as a consequence) instead of the traditional ...
  • 187
5 votes
1 answer
364 views

Is private global mutable state ever appropriate, namely when used to prevent API misuse?

I am writing a type checker for an ML dialect that involves generating "fresh" (new and unique) "type variables" (values representing unknowns). My strategy, and the strategy that seems to be used in ...
  • 161
5 votes
1 answer
2k views

How to properly extend an interface with immutable properties to offer mutability via another interface

The following code snippets are simplified to demonstrate the context! The actual interfaces and classes are POCOs having additional properties. The types are part of library I am working on, the ...
  • 51
0 votes
2 answers
107 views

Is it a different object or the same object with an altered property (mutable or not)?

When I design the architecture of an application, I usually think about the nature of the objects and the interactions between them. The way I currently think about mutability is to ask myself if ...
  • 2,551
6 votes
1 answer
6k views

Is it pythonic to use properties to limit the mutability of class attributes (variables and methods)?

Some Explanation I'm somewhat new to python and to programming (I've been at it for a little over a year). I just recently discovered python properties, and I've been using them to limit the ...
0 votes
1 answer
150 views

Mutability, and pass-by-reference, "new" object, what am I having left out?

I started questioning what I know, or thought that I know, after this question: Array.fill differs from literal 2D definition on assignment A JavaScript question, defining an array with predefined ...
56 votes
3 answers
36k views

Is it an antipattern to use peek() to modify a stream element?

Suppose I have a stream of Things and I want to "enrich" them mid stream, I can use peek() to do this, eg: streamOfThings.peek(this::thingMutator).forEach(this::someConsumer); Assume that ...
  • 1,896
2 votes
2 answers
1k views

Why does an immutable object in Scala need less memory than a mutable one?

In Programming in Scala, there is this statement: For instance an empty mutable map in its default representation of HashMap takes up about 80 bytes and about 16 more are added for each entry that’...
1 vote
2 answers
136 views

Is it considered good practice to use mutables this way? [closed]

Lately I came across a way of using mutables which I had not thought of before. Let's say you have a list comprehension which calls a method, and you put the result in a list like the following (...
  • 315
3 votes
1 answer
410 views

Encapsulating mutable objects with special structure in Python

I am writing a library for working with special types of trees, called Foo trees. A Foo tree has very special structure. There some operations, called bar and baz, which only make sense on Foo trees. ...
  • 549
1 vote
3 answers
1k views

Addition or deletion of elements of a dynamic array

Is there any consensus among programmers (or a common convention) on the "right way" to deal with the addition or deletion of one or more elements of a dynamic (mutable) array at runtime while ...
  • 119
3 votes
4 answers
879 views

How to refactor my project to have less mutable objects? [duplicate]

There seems to be a trend towards immutable objects, and functional programming. I recently got aware about the benefits of immutability. However, I am not very familiar with this style of programming....
  • 1,806
54 votes
9 answers
5k views

Is immutability very worthwhile when there is no concurrency?

It seems that thread-safety is always/often mentioned as the main benefit of using immutable types and especially collections. I have a situation where I would like to make sure that a method will ...
  • 4,829
0 votes
1 answer
370 views

Is it a bad idea to use mutable for pre-allocated temporaries?

Let us assume I have the following C++ class: class MyFastMessageEncoder { public: MyFastMessageEncoder() : m_fieldEncoder(ENCODING_STYLE_DEFAULT) { } void set_encoding_style(...
8 votes
2 answers
4k views

Overriding GetHashCode in a mutable struct - What NOT to do?

I am using the XNA Framework to make a learning project. It has a Point struct which exposes an X and Y value; for the purpose of optimization, it breaks the rules for proper struct design, since its ...
3 votes
2 answers
366 views

Why must essential mutable derived data have an inverse function?

I was reading the paper Out of the Tar Pit authored by Ben Moseley and Peter Marks when I came across the following section on page 25 regarding essential mutable derived data: Essential Derived Data ...
  • 1,598
0 votes
2 answers
114 views

Mutable cursor in readonly object?

I've been searching for the different uses of the keyword mutable. I've found that it is generally used for caching, lazy computing, mutex, ... But I'm wondering if it is coherent to use it for a ...
  • 9
3 votes
2 answers
666 views

subclass of immutable object not immutable, can this work?

So I'm finishing up refactoring some code to remove a number of previously-mutable objects and add a better generic processing for all the classes in the domain. Just as I thought I was finishing I ...
  • 1,125
-1 votes
4 answers
2k views

Immutable vs mutable object as returned parameter for class method [closed]

There is a class method (static method) in which I create and build some object. And for filling that object, I create it as mutable object. My mutable object is a subclass of immutable object. So ...
  • 129
6 votes
4 answers
20k views

Is using "out" or "ref" parameters in Java methods to return extra values bad?

I happened to create a mutable class like this: class Mutable<T> { private T value; public Mutable() { this.value = null; } public Mutable(T value) { this.value = value; } T get(...
  • 3,734