简体   繁体   English

你期望不可变列表的不变性有多深?

[英]How deep would you expect the immutability of an immutable list to be?

If you have an immutable list, you expect it to always return a reference to the same object when you ask for, say 如果你有一个不可变列表,你希望它总是在你要求的时候返回对同一个对象的引用

list.get(0)

My question is, would you expect to be able to mutate the object and have the mutation reflected next time you get it from the list? 我的问题是,您是否希望能够改变对象并在下次从列表中获取突变时反映出变异?

It depends on the context. 这取决于具体情况。 In a general purpose library, all we should assume is that the list is immutable. 在通用库中,我们应该假设列表是不可变的。 Changes to the elements in the list would be reflected to all callers, as a direct consequence of returning the same reference each time. 列表中的元素的变化将反映到所有呼叫者,如每次返回相同的参考的直接后果。

However, if this is a specialized immutable tree (or whatever), and is documented as such then you would expect the items in the list to themselves be immutable, and it becomes a moot question. 但是,如果这是一个专门的不可变树(或其他), 并且记录如此,那么你会希望列表中的项本身是不可变的,这就成了一个没有实际意义的问题。

The question if not about the immutability of the list, but about the immutability of the objects contained. 如果不是关于列表的不变性,而是关于所包含对象的不变性的问题。

In fact, if you have reference types, the immutable entity in the list is the reference. 实际上,如果您有引用类型,则列表中的不可变实体是引用。 This means that the reference will always be the same. 这意味着引用将始终相同。 Now whether the referenced object changes only depends on what kind of object it is. 现在引用的对象是否只发生变化取决于它是什么类型的对象。 If the object is immutable (like, for instance, strings in both .NET and Java, or all value types in .NET), the object cannot change. 如果对象是不可变的(例如,.NET和Java中的字符串,或.NET中的所有值类型),则对象不能更改。

Otherwise, the object can change and all other references to the same object will see the changed state, since they hold a reference to the same instance. 否则,对象可以更改,并且对同一对象的所有其他引用将看到更改的状态,因为它们包含对同一实例的引用。 Therefore, as I wrote in the beginning, this is completely independent of the list (and whether it is immutable or not). 因此,正如我在开头所写的那样,这完全独立于列表(以及它是否是不可变的)。

Suppose a repair shop wanted to keep a permanent append-only record of all the cars that had ever visited, so that as each car entered the owners could find out if it had been in the shop before. 假设一家维修店想要保留所有曾经访问过的汽车的永久附加记录,这样每辆车进入车主时都可以知道它是否曾经在车间。 Which would be better: 哪个更好:

  1. Keep a permanent record of the VIN (Vehicle Identification Number) of each car 保留每辆车的VIN(车辆识别号码)的永久记录
  2. Make a duplicate of each car (which, because VINs must be unique, must have a different VIN from the original) and permanently store the duplicate cars. 制作每辆车的副本(因为VIN必须是唯一的,必须具有与原件不同的VIN)并永久存储重复的车辆。

Note that a car itself is a mutable object, but the identity of a car, expressed by the VIN, is immutable. 请注意,汽车本身是一个可变对象,但由VIN表示的汽车的身份是不可变的。 It's entirely possible that a car which was blue when it visited the shop has since been painted red. 完全可能的是,一辆汽车在参观商店时是蓝色的,因此被漆成了红色。 Thus, even if one had the ability to easily locate any car given its VIN, a list of cars (VINs) and when they were in the shop would not allow one to determine eg how many blue cars were serviced last Thursday. 因此,即使有人能够轻松找到任何有VIN的汽车,汽车列表(VIN)以及他们在车间的时间也不允许人们确定上周四有多少辆蓝色汽车在维修。 On the other hand, if the purpose of the list is to let one know whether an incoming vehicle had been in the shop previously, the list of VINs is exactly what one would need. 另一方面,如果列表的目的是让人知道先前车辆是否已经进入车间,那么VIN列表正是人们所需要的。 If instead of having the VINs, one had a collection of duplicate cars, then not only would the cost of creating and storing all those duplicate cars be far greater than the cost of storing the VINs, but the collection would be almost useless for the stated purpose (determining whether a particular car had visited previously). 如果不是拥有VIN,而是拥有一系列重复的汽车,那么创建和存储所有这些复制汽车的成本不仅远远大于存储VIN的成本,而且收集对于所述的收集几乎是无用的。目的(确定某辆车之前是否曾访问过)。

That's usually to be expected. 这通常是预料之中的。 The list is immutable, which means you cannot add or remove items in it or replace items entirely. 该列表是不可变的,这意味着您无法在其中添加或删除项目或完全替换项目。 If you want those items to be immutable you have to take care of that yourself. 如果您希望这些项目是不可变的,您必须自己处理。 The list certainly can't stop you from mutating the object's state once you got a reference to it. 一旦你得到对象的引用,该列表肯定无法阻止你改变对象的状态。

Yes. 是。

I don't expect an immutable list to clone its objects when I get them, unless it is documented as doing so. 当我得到它们时,我不希望不可变列表克隆它的对象,除非它被记录为这样做。

It really depends on the context in which you ask that question. 这实际上取决于你提出这个问题的背景。 Any experienced Java or C# developer knows that it's technically almost impossible to have a general "deep immutability" and therefore would not expect this. 任何有经验的Java或C#开发人员都知道技术上几乎不可能拥有一般的“深层不变性”,因此不会期望这样。 In C++, it's a very complex topic , so most developers probably also don't expect a dependable deep immutability. 在C ++中,这是一个非常复杂的主题 ,因此大多数开发人员可能也不期望可靠的深层不变性。 The D programming language, on the other hand, does have a language-level concept of transitive immutability, so a D programmer would probably expect it wherever it makes sense (which is quite often). 另一方面,D编程语言确实具有传递不变性的语言级概念 ,因此D程序员可能期望它在任何有意义的地方(这经常是)。

Yes, knowing Java, for an "immutable" List<T> I wouldn't expect T to be immutable unless T was immutable. 是的,知道Java,对于一个“不可变的” List<T>我不希望T是不可变的,除非T是不可变的。 However, a reasonable implementation of, say, List<Date> would be to copy the Date each time. 但是,例如List<Date>的合理实现是每次复制Date The problem is that Date is mutable and can be distinguished from other equal Date s. 问题是Date是可变的,可以与其他相等的Date区分开来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM