问题标签 [mutable]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
8688 浏览

c++ - C ++中的静态可变成员变量?

为什么或出于什么原因不能在 C++ 中将类成员变量声明为static mutable?就像是

对我来说,没有理由禁止这样的声明。例如,出于维护全局类范围的统计信息等原因,拥有可以通过(逻辑上)const 方法更改的静态变量可能很方便。因此,这要么是 C++ 中的一种错误设计并且不必要地复杂,要么存在我看不到的实际或理论原因。

0 投票
3 回答
4052 浏览

scala - Scala中的可变方法参数

简单的问题:我在 Scala 中继承了一个 FilterInputStream,它有一个 read 方法:

正在读取的数据将放在 b 中,但由于参数是 Scala 方法中的“vals”,我看不到正确子类化它的方法。如何将 b 设置为正在读取的数据?Java *InputStream 真的让我没有太多选择......

0 投票
1 回答
448 浏览

java - 这是 Clojure 中可变状态的明智单子吗?

我一直在用 Clojure 中的 monad 进行试验,并提出了以下代码,其中一个 monadic 值/状态对由一个可变的 Clojure deftype 对象表示。

由于对象是可变的,一个优点似乎是您可以编写一元代码,而无需一直构造新的结果对象。

但是,我对 monads 很陌生,所以很想知道:

  • 这种结构有意义吗?
  • 它真的可以作为 monad 正常工作吗?

下面的代码:

0 投票
7 回答
127292 浏览

scala - 向 scala.collection.mutable.Map 添加元素的语法是什么?

向 scala.collection.mutable.Map 添加元素的语法是什么?

以下是一些失败的尝试:

0 投票
4 回答
223 浏览

java - 在java中获取一个字符串作为参考

所以我希望能够在 Java 中拥有一组可变字符串。

我有这个测试类来查看不可变字符串的功能:

以下是输出:

我该怎么做才能将这些字符串存储为可变的?我正在考虑有一个 StringObject 类,它只包含对 String 的引用。这是最好的选择吗?

0 投票
3 回答
5124 浏览

c++ - 如何定义指向 const 对象的可变指针?

我有一个类,它包含一个指向类外部常量 VARIANT 值的指针,但有时我想更改此指针以引用类本身的 VARIANT 成员对象。

这个类的大多数实例都是常量,所以我必须将指针声明为可变的。

在 Visual C++ 中,这段代码似乎符合我的要求:

但是,由于 mutable 是指针的属性而不是指针的属性,我认为这是正确的语法:

类似于定义常量指针(而不是指向 const 对象的指针)。但是,Visual C++ 不接受这种变体。

警告 C4518:“可变”:此处意外的存储类或类型说明符;忽略

Visual C++ 是对的,还是我遗漏了什么?另一个更符合标准的编译器会表现得不同吗?

0 投票
1 回答
2208 浏览

java - Spring setter/getter 和可变变量

让我们建议我有以下课程

对 Spring 或 Hibernate Framework 使用可变的 getter 和 setter 并在我的自定义编码中使用不可变的 setter 和 getter 是否是一种好习惯。我害怕的是以下几点:

现在发生了什么,我刚刚更改了 myClass 对象中日期变量的值,它可能是系统中错误的主要根源。

我对么?我也可以为 Spring/Hibernate 使用不可变的 getter 和 setter 吗?

0 投票
2 回答
22899 浏览

c++ - 互斥体应该是可变的吗?

不确定这是一个风格问题,还是有硬性规定的问题......

如果我想尽可能保持公共方法接口为 const,但使对象线程安全,我应该使用可变互斥锁吗?一般来说,这是一种好的风格,还是应该首选非常量方法接口?请证明你的观点。

0 投票
5 回答
1810 浏览

constants - D中的逻辑常量

D 有两种 const 类型:不可变变量是被声明为不可变的变量,并且始终是不可变的,而const变量只是对象的只读版本。

逻辑 const是指函数被标记为const,但允许对一个或多个成员变量进行写访问。它的典型用途是惰性求值,例如(在 C++ 中)

在这里,determinant()const,但仍然可以修改m_dirty,并且m_determinant由于它们被标记为mutable

D const(FAQ)说 D2 不支持逻辑 const,因为它提供的保证很弱,这阻碍了编写并发程序,并使某些优化更加困难。

我完全理解这个问题,但是如果我们需要逻辑常量怎么办?

考虑上面带有Matrix类的情况,但没有缓存(并且不需要任何逻辑常量)。还可以想象这个类在我的代码库中使用,并且主要通过 const 引用访问。

现在考虑分析表明该determinant()函数是代码中的瓶颈,此外,它通常被重复访问,其值很少改变,即缓存,如上所述,将是一个完美的优化。

如果没有逻辑常量,我怎么能做到这一点?遍历我的代码库将 const 引用更改为非 const 引用不是一种选择(出于显而易见的原因)。

我有哪些选择(如果有)?

0 投票
8 回答
17643 浏览

c++ - Correct alternative to a 'mutable function' in c++

I often have a problem with const correctness when wrapping algorithms in classes in c++. I feel that I want a mutable function, although this is not allowed. Can anyone advise me how to implement classes such as the one that follows?

The following is the code that I want to write.

  • The function run() should not be a const function because it changes the data.
  • The function get_result() should be a constant function (as far as the user is concerned) because it returns the data.

However, if the user requests the result without calling run(), I want the get_result() function to run the algorithm. This breaks the const correctness because I have a const function calling a non-const function.

The only way I have been I have managed to compile the above class is to either

  • make run() const, and make m_data and m_completed mutable. This works but is conceptually wrong because run() demonstrably changes data.
  • make get_result() not a constant function. This seems wrong too, for the user would expect this function to be a simple return, and therefore constant.
  • Put the run() function into the get_result() const function and make the data variables mutable.

My understanding was that the mutable keyword was designed for the third of these options, where the implementation requires data to be changed but the user reasonably expects a simple return and therefore const function.

However, I don't want to do this final option because I want the user to be able to choose exactly when they change the data. There is a chance that they will forget to call run(), however, and so I want to force the algorithm if they request the result without calling run(). I feel like a want to make run() mutable - but I'm not allowed to.

What is the correct way to write such a class?