你希望不可变的列表的不变性有多深? [英] 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.
这篇关于你希望不可变的列表的不变性有多深?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!