对不起,我的标题太含糊了——如果我能想出一个简洁的标题,我就不用问这个问题了。

假设我有一个不可变的列表类型。它有一个操作Foo(x),该操作返回一个新的不可变列表,其中指定的参数作为结尾的额外元素。因此,要建立一个包含"Hello", "immutable", "world"值的字符串列表,你可以这样写:

var empty = new ImmutableList<string>();
var list1 = empty.Foo("Hello");
var list2 = list1.Foo("immutable");
var list3 = list2.Foo("word");

(这是c#代码,我最感兴趣的是c#建议,如果你觉得语言很重要的话。这根本不是一个语言问题,但语言中的习语可能很重要。)

重要的是现有的列表不会被Foo改变,所以是空的。Count仍然返回0。

另一种(更习惯的)达到最终结果的方式是:

var list = new ImmutableList<string>().Foo("Hello")
                                      .Foo("immutable")
                                      .Foo("word");

我的问题是:Foo最好的名字是什么?

编辑3:正如我稍后所揭示的,类型的名称实际上可能不是ImmutableList<T>,这使得位置很清楚。相反,想象它是TestSuite,它是不可变的,因为它所在的整个框架是不可变的……

(编辑3结束)

到目前为止,我想到的选项是:

Add: common in .NET, but implies mutation of the original list Cons: I believe this is the normal name in functional languages, but meaningless to those without experience in such languages Plus: my favourite so far, it doesn't imply mutation to me. Apparently this is also used in Haskell but with slightly different expectations (a Haskell programmer might expect it to add two lists together rather than adding a single value to the other list). With: consistent with some other immutable conventions, but doesn't have quite the same "additionness" to it IMO. And: not very descriptive. Operator overload for + : I really don't like this much; I generally think operators should only be applied to lower level types. I'm willing to be persuaded though!

我选择的标准是:

给出方法调用结果的正确印象(即它是带有额外元素的原始列表) 尽可能清楚地表明它不会改变现有的列表 当像上面的第二个例子一样连接在一起时,听起来很合理

如果我说得不够清楚,请再问我一些细节。

编辑1:以下是我更喜欢Plus而不是Add的理由。考虑以下两行代码:

list.Add(foo);
list.Plus(foo);

在我看来(这是我个人的观点),后者显然是有问题的——它就像写“x + 5;”作为一个单独的语句。第一行看起来是可以的,直到您记得它是不可变的。事实上,加号操作符本身不改变操作数的方式是我最喜欢加号的另一个原因。没有操作符重载的麻烦,它仍然具有相同的含义,其中包括(对我来说)不改变操作数(在本例中是方法目标)。

编辑2:不喜欢添加的原因。

各种各样的回答都很有效:“使用Add。这就是DateTime所做的,而String有Replace方法等,这些方法不会使不可变性变得明显。”我同意,这里有优先权。然而,我见过很多人称之为DateTime。添加或字符串。替换并期待突变。有大量的新闻组问题(如果我仔细研究的话,可能会有这样的问题),这些问题的答案是“您忽略了String.Replace的返回值;字符串是不可变的,返回一个新的字符串"

现在,我应该揭示这个问题的一个微妙之处——类型实际上可能不是一个不可变列表,而是一个不同的不可变类型。特别是,我正在开发一个基准测试框架,在该框架中,您可以向套件中添加测试,并创建一个新的套件。这可能是显而易见的:

var list = new ImmutableList<string>();
list.Add("foo");

不会完成任何事情,但当你把它改成:

var suite = new TestSuite<string, int>();
suite.Add(x => x.Length);

看起来应该没问题。然而,对我来说,这个错误更清楚了:

var suite = new TestSuite<string, int>();
suite.Plus(x => x.Length);

这只是在乞求:

var suite = new TestSuite<string, int>().Plus(x => x.Length);

理想情况下,我希望我的用户不必被告知测试套件是不可变的。我想让他们坠入成功的深渊。这可能是不可能的,但我想试试。

很抱歉,我只讨论了不可变的列表类型,过度简化了最初的问题。并非所有的集合都像ImmutableList<T>:)那样具有自描述性。


当前回答

This seems to come down to finding a word that expresses that the object is not modified. Also, that it is cloned and the element passed as a parameter is added to the end of the cloned list. Standard names like add and clone are never going to cover it. Perhaps clone_and_append which is also rather ambiguous, since the append part might imply that the parameter is appended to the original list. So it should probably be something like clone_and_append_to_clone or better yet append_to_clone, although this one does not really imply that the clone is going to be returned by this method, but rather that the clone already exists as part of the origina list.

因此,更多地考虑寻找一个不意味着对原始列表进行修改的名称,并建议创建一个新列表,考虑以下内容:

Offshoot -- var list1 = list.Offshoot("tail") - in the strictest sense, offshoot refers here to the new element and not the entire new list. Another apparent disadvantage is that it is not an action, the more correct name being list.CreateOffshoot. However, I find it gives a clear indication of the fact that the new list will contain the original list and that the new element will come at the end of the list. Spawn -- var list1 = list.Spawn("tail") - similar to the previous. The advantage is that it is an action, but there is a weaker implication that the new list will contain the original list. BranchOff -- var list1 = list.BranchOff("tail") - an action that suggests the original list will be cloned. One potential ambiguity is that it is not very clear how the parameter element will be used.

其他回答

我认为Plus()和Minus()或include()、excluded()在暗示不可变行为方面是合理的。

然而,任何命名选择都不会让每个人都完全清楚,所以我个人认为,一个好的xml文档注释将在这里发挥很大作用。当你在IDE中编写代码时,VS会把这些问题扔到你的脸上——它们很难被忽视。

加入似乎是合适的。

我觉得"加"或"加"听起来不错。列表本身的名称应该足以传达列表的不可变性。

我会使用构造函数。

Foo f1 = new Foo("one");
Foo f2 = new Foo(f1, "two");
Foo f3 = new Foo(f2, "three");

F1包含“1”。 F2包含“一”、“二”。 F3包含“one”、“two”、“three”。

也许有一些词让我记得更多的是复制和添加东西,而不是突变实例(如“Concatenate”)。但我认为在其他行为上有一些对称的话也会很好。我不知道一个类似的词“删除”,我认为类似于“连接”。“加上”对我来说听起来有点奇怪。我不希望它用于非数值上下文中。但这也可能是因为我的非英语背景。

也许我会用这个方案

AddToCopy
RemoveFromCopy
InsertIntoCopy

不过,当我想到这些时,它们也有自己的问题。人们可能会认为他们在给定的论证中删除了一些东西或添加了一些东西。我一点也不确定。我认为,这些话也不适合用在捆绑上。太啰嗦了,没法打字。

也许我会用简单的“添加”和“朋友”。我喜欢它在数学中的应用

Add 1 to 2 and you get 3

Well, certainly, a 2 remains a 2 and you get a new number. This is about two numbers and not about a list and an element, but i think it has some analogy. In my opinion, add does not necessarily mean you mutate something. I certainly see your point that having a lonely statement containing just an add and not using the returned new object does not look buggy. But I've now also thought some time about that idea of using another name than "add" but i just can't come up with another name, without making me think "hmm, i would need to look at the documentation to know what it is about" because its name differs from what I would expect to be called "add". Just some weird thought about this from litb, not sure it makes sense at all :)