- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
让我们考虑封装fileName
的不可变对象(immutable对象)File
,它指向磁盘上的文件。该对象具有方法 rename
,这显然是为磁盘上的文件分配新名称。
public final class File {
private final String fileName;
public File(String fileName fileName) {
this.fileName = fileName;
}
public File rename(String newName) {
// Rename the actual file on the disk (code is omitted)
// and that is the side effect!
return new File(newName);
}
}
所以问题是:
File
对象一旦创建,就应该始终以相同的封装状态工作。并返回具有修改状态的新对象。这在 java.lang.String
类中完美地完成了。但是旧对象(仍然指向错误 fileName
)并不好。我们无法使用它,因为状态已损坏。假设 File
只是一个例子。
最佳答案
How to approach side effects with immutable objects? Our File object once created should be working always with the same encapsulated state. And return new object with modified state. This is perfectly done in java.lang.String class. But the old object (which is still point to wrong fileName) is not good. We can't use it as state is broken.
您的示例存在一个无法克服的问题。你的副作用,这部分代码,不在你的控制范围内:
// Rename the actual file on the disk (code is omitted)
// and that is the side effect!
如果是 String,字符串的每个字节都在您的控制之下,因此您可以保证 String 上的方法的纯度。对于您的 File 示例,还涉及一个环境 - 操作系统,该文件是否存在。即使它存在,您的进程也可能无法访问它。您实际上无法重命名文件的原因可能有很多。
IMO 不可能克服这个问题。你周围的世界并不纯粹——你的代码在系统上执行时有很多副作用和可变性。您的磁盘是有状态的,您的 RAM 不是恒定的。即使在 Haskell 中,函数 renameFile::FilePath -> FilePath -> IO ()
来自 https://hackage.haskell.org/package/directory-1.3.1.1/docs/System-Directory.html不是理想的纯净 - 在某些情况下可能会导致错误。
但这并不是认为对象无效的理由。正如评论中已经说过的那样,您封装了文件名,而不是文件描述符。因此,您的 File 代表有效的文件名。可能问题更多是关于重命名方法的有效性?
Does it means that object is not truly immutable? And what if we want to work only with true immutable objects with consistent state? We shouldn't rename file on the disk? But how to get the job done when we actually need file to be renamed?
它是不可变的。对该对象的调用 rename
并不纯粹,仅此而已。 不变性 和纯度 是不同的术语。您的对象可能是不可变的并且仍然有副作用,就像您的示例一样。
关于java - 具有副作用的不可变对象(immutable对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45631906/
为什么禁用类型像 type t = A of int | B of string * mutable int 虽然允许此类类型: type t = A of int | B of string * i
我正在寻找一种类似结构的数据结构,我可以从中创建多个实例并具有某种类型提示而不是不可变的。 所以我有这样的东西: class ConnectionConfig(NamedTuple): nam
我需要转到引用的结构: class SearchKnot { var isWord : Bool = false var text : String = "" var to
如sec 10.4.3中所述 当控制进入执行时,执行以下步骤 功能对象F(调用者)中包含的功能代码的上下文 提供thisArg,而调用方提供argumentsList: 如
i make a game that start display Activity indicator And activity indicator bottom display UiLable wi
编辑:我在这里不断获得支持。只是为了记录,我认为这不再重要。自从我发布它以来我就不再需要它了。 我想在 Scala 中执行以下操作... def save(srcPath: String, destP
使用可变对象作为 Hashmap 键是一种不好的做法吗?当您尝试使用已修改足以更改其哈希码的键从 HashMap 中检索值时,会发生什么? 例如,给定 class Key { int a; /
如果您在Kotlin中访问List类型的Java值,则将获得(Mutable)List!类型。 例如。: Java代码: public class Example { public stati
我编写了 str 类(内置)的以下扩展,以便执行以下操作:假设我有字符串 "Ciao" ,通过做"Ciao" - "a"我想要的结果是字符串 "Cio" 。这是执行此操作的代码,并且运行良好: cla
使用可变对象作为 Hashmap 键是一种不好的做法吗?当您尝试使用已修改足以更改其哈希码的键从 HashMap 中检索值时,会发生什么? 例如,给定 class Key { int a; /
我正在为我的公司设计一个数据库来管理商业贷款。每笔贷款都可以有担保人,可以是个人或公司,在借款业务失败时作为财务支持。 我有 3 个表:Loan、Person 和 Company,它们存储明显的信息。
我使用二进制序列化从 C# 类中保存 F# 记录。一切正常: F#: type GameState = { LevelStatus : LevelStatus
import javax.swing.JOptionPane; public class HW { public static void main(String[] args) { Strin
使用 flatbuffer mutable 有多少性能损失? 是否“正确”使用 FlatBuffers 来拥有一个应该可编辑的对象/结构(即游戏状态) 在我的示例中,我现在有以下类: class Ga
std::function create_function (args...) { int x = initial_value (args...); return [x] () mut
我需要在 for 循环中找到用户输入的字符。我通常会这样做 如果(句子[i] == 'e') 但是因为在这里,'e' 将是一个单字母字符变量,我不知道如何获取要比较的值。我不能只输入 if (sent
我有一个这样的算法: let seed: Foo = ... let mut stack: Vec = Vec::new(); stack.push(&seed); while let Some(ne
这个问题可能看起来非常基础,但我很难弄清楚如何做。我有一个整数,我需要使用 for 循环来循环整数次。 首先,我尝试了—— fn main() { let number = 10; // An
如果我有以下结构: struct MyStruct { tuple: (i32, i32) }; 以及以下函数: // This will not compile fn function(&mut s
我希望在每个 session 的基础上指定列的默认值。下面的脚本不起作用,但描述了我想如何使用它。我目前使用的是 MySQL 5.5.28,但如果需要可以升级。 CREATE TABLE my_tbl
我是一名优秀的程序员,十分优秀!